#include<stdio.h>
#include<algorithm>
#include <map>
#include <list>
#include <stack>
#include <queue>
#include <vector>
#include <iostream>
#include <functional>
#include <time.h>
using namespace std;
clock_t fi, endtime;

const int INF = 100 * 1000 + 2;
const int r_c = 0;
const int r_t = 1;
const int MAX_V = 100;
const int MAX_E = 6000;

int n, m, a, b, c, t;
int k, p, q, r;

int wft[MAX_V][MAX_V] = { 0 };
int wfc[MAX_V][MAX_V] = { 0 };

int main()
{
	scanf_s("%d %d", &m, &n);
	for (int i = 0; i < n; ++i) {
		for (int j = 0; j < n; ++j) {
			wfc[i][j] = INF; wft[i][j] = INF;
		}
	}

	for (int i = 0; i < m; ++i) {
		scanf_s("%d %d %d %d", &a, &b, &c, &t);
		wft[a - 1][b - 1] = t;  wft[b - 1][a - 1] = t;
		wfc[a - 1][b - 1] = c;  wfc[b - 1][a - 1] = c;
	}
	for (int k = 0; k < n; ++k) {
		for (int i = 0; i < n; ++i) {
			for (int j = 0; j < n; ++j) {
				if (wft[i][j] > wft[i][k] + wft[k][j]) { wft[i][j] = wft[i][k] + wft[k][j]; }
				if (wfc[i][j] > wfc[i][k] + wfc[k][j]) { wfc[i][j] = wfc[i][k] + wfc[k][j]; }
			}
		}
	}
	scanf_s("%d", &k);
	int ans = 0;
	for (int i = 0; i < k; ++i){
		scanf_s("%d %d %d", &p, &q, &r);
		if (p == 0 && q == 0) break;
		if (r == 0) { ans = wfc[p - 1][q - 1]; }
		else { ans = wft[p - 1][q - 1]; }
		printf("%d\n", ans);
	}

}