#include<bits/stdc++.h>
#define for1(i, n) for(int i = 1; i <= (n);i++)
using namespace std;
int a0[110][110], a1[110][110], n, m;
signed main() {
	while (cin >> n >> m, n) {
		for1(i, m)for1(j, m) { a0[i][j] = n * 1001; a1[i][j] = n * 1001; }
		for1(l0y3r, n) {
			int a, b, c, d; cin >> a >> b >> c >> d;
			a0[a][b] = c; a0[b][a] = c;
			a1[a][b] = d; a1[b][a] = d;
		}
		for1(k, m)for1(i, m)for1(j, m) {
			a0[i][j] = min(a0[i][j], a0[i][k] + a0[k][j]);
			a1[i][j] = min(a1[i][j], a1[i][k] + a1[k][j]);
		}
		cin >> n;
		for1(l0y3r, n) {
			int p, q, r; cin >> p >> q >> r;
			if (r)cout << a1[p][q] << endl;
			else cout << a0[p][q] << endl;
		}
	}
}
