#include <iostream>
#include <algorithm>

using namespace std;

int main() {
	int n, m, k;

	while(cin >> n >> m ) {

	if (n == 0 && m == 0) return 0;

	int **costs = new int*[n];
	int **times = new int*[n];
	for(int i = 0; i < n; i++) {
		costs[i] = new int[n];
		times[i] = new int[n];
		for(int j = 0; j < n; j++) {
			costs[i][j] = 1e5;
			times[i][j] = 1e5;
		}
	}

	for(int i = 0; i < n; i++) {
		int a, b, cost, time;
		cin >> a >> b >> cost >> time;
		a -= 1;
		b -= 1;
		costs[a][b] = costs[b][a] = cost;
		times[a][b] = times[b][a] = time;
	}

	for(int i = 0; i < n; i++) {
		for(int j = 0; j < n; j++) {
			for(int k = 0; k < n; k++) {
				costs[j][k] = min(costs[j][k], costs[j][i] + costs[i][k]);
				times[j][k] = min(times[j][k], times[j][i] + times[i][k]);
			}
		}
	}

	cin >> k;
	for(int i = 0; i < k; i++) {
		int p, q, r;
		cin >> p >> q >> r;
		p -= 1;
		q -= 1;
		if(r) cout << times[p][q];
		else cout << costs[p][q];
		cout << endl;
	}
	}
	return 0;
}