#include<iostream>
using namespace std;

#define M 101
#define INF 100000003

#define min(a,b) (a<b?a:b)

int main() {
	int n, m, a, b, c, t, pn, p, q, r;
	int dc[M][M], dt[M][M];
	while(1) {
		cin >> n >> m;
		if(!n&&!m) break;
		for(int i=1; i<=m; ++i) {
			for(int j=1; j<=m; ++j) {
				dc[i][j] = dt[i][j] = INF;
			}
		}
		for(int i=0; i<n; ++i) {
			cin >> a >> b >> c >> t;
			dc[a][b] = dc[b][a] = c;
			dt[a][b] = dt[b][a] = t;
		}
		for(int k=1; k<=m; ++k) {
			for(int i=1; i<=m; ++i) {
				for(int j=1; j<=m; ++j) {
					dc[i][j] = min(dc[i][j], dc[i][k]+dc[k][j]);
					dt[i][j] = min(dt[i][j], dt[i][k]+dt[k][j]);
				}
			}
		}
		cin >> pn;
		for(int i=0; i<pn; ++i) {
			cin >> p >> q >> r;
			cout << (!r ? dc[p][q] : dt[p][q]) << endl;
		}
	}
	return 0;
}