#include<iostream>
#include<algorithm>
using namespace std;
int main() {
	int rp;
	rp = 0;
	int ans[111][333];
	int f[1111], aa, bb, cc;
	int n,a,b,c,d, time[111][111],m, cost[111][111];
	while (1) {
		cin >> n >> m;
		if (n == 0 && m == 0) {
			goto a;
		}
		rp = rp + 1;
		for (int i = 1; i <= m; i++) {
			for (int z = 1; z <= m; z++) {
				if (z == i) {
					time[z][i] = 0;
					time[i][z] = 0;
					cost[i][z] = 0;
					cost[z][i] = 0;
				}
				else {
					time[z][i] = 9999;
					time[i][z] =9999;
					cost[i][z] = 9999;
					cost[z][i] = 9999;
				}
			}
		}
		for (int i = 1; i <= n; i++) {
			cin >> a >> b >> c >> d;
			cost[a][b] = c;
			cost[b][a] = c;
			time[a][b] = d;
			time[b][a] = d;
		}
		for (int i = 1; i <= m; i++) {
			for (int z = 1; z <= m; z++) {
				for (int s = 1; s <= m; s++) {
					cost[z][s] = min(cost[z][s], cost[z][i] + cost[i][s]);
					time[z][s] = min(time[z][s], time[z][i] + time[i][s]);
				}
			}
		}
		cin >> f[rp];
		for (int i = 1; i <= f[rp]; i++) {
			cin >> aa >> bb >> cc;
			if (cc == 1) {
				ans[rp][i] = time[aa][bb];
			}
			else {
				ans[rp][i] = cost[aa][bb];
			}
		}
	}
a:;
	for (int z = 1; z <= rp; z++) {
		for (int i = 1; i <= f[z]; i++) {
			cout << ans[z][i] << endl;
		}
	}
}
