#include<bits/stdc++.h>

using namespace std;

int N, M;
int DpCost[101][101], DpTime[101][101];
const int INF = 1e8;


void solve() {
	for (int k = 1; k <= M; ++k) {
		for (int i = 1; i <= M; ++i) {
			for (int j = 1; j <= M; ++j) {
				if (i == j) {
					DpCost[i][j] = DpTime[i][j] = 0;
					continue;
				}
				DpCost[i][j] = min(DpCost[i][j], DpCost[i][k] + DpCost[k][j]);
				DpTime[i][j] = min(DpTime[i][j], DpTime[i][k] + DpTime[k][j]);
			}
		}
	}
}

int main() {
	int a, b, cost, time, k, r;

	while (cin >> N >> M, N) {
		for (int i = 1; i <= M; ++i) {
			fill(DpCost[i], DpCost[i] + 101, INF);
			fill(DpTime[i], DpTime[i] + 101, INF);
		}
		for (int i = 0; i < N; ++i) {
			cin >> a >> b >> cost >> time;
			DpCost[a][b] = DpCost[b][a] = cost;
			DpTime[a][b] = DpTime[b][a] = time;
		}
		solve();
		cin >> k;
		for (int i = 0; i < k; ++i) {
			cin >> a >> b >> r;
			if (r == 0) {
				cout << DpCost[a][b] << endl;
			}
			else {
				cout << DpTime[a][b] << endl;
			}
		}
	}

	return 0;
}
