#include <iostream>
#include <cstdio>
using namespace std;

int main(void)
{
	int n, m, a, b, c, t;
	int p, q, r;
	int cost[100][100], time[100][100];
	
	while (scanf("%d %d", &m, &n), n){
		for (int i = 0; i < 100; i++){
			for (int j = 0; j < 100; j++){
				if (i == j) cost[i][j] = time[i][j] = 0;
				else cost[i][j] = time[i][j] = 10000000;
			}
		}
		
		for (int i = 0; i < m; i++){
			cin >> a >> b >> c >> t;
			cost[a - 1][b - 1] = c;
			time[a - 1][b - 1] = t;
		}
		
		for (int k = 0; k < n; k++){
			for (int i = 0; i < n; i++){
				for (int j = 0; j < n; j++){
					cost[i][j] = min(cost[i][j], cost[i][k] + cost[k][j]);
					time[i][j] = min(time[i][j], time[i][k] + time[k][j]);
				}
			}
		}
		
		int k;
		cin >> k;
		for (int i = 0; i < k; i++){
			cin >> p >> q >> r;
			if (r == 0) cout << cost[p - 1][q - 1] << endl;
			else cout << time[p - 1][q - 1] << endl;
		}
	}
	
	return 0;
}