#include <iostream>

#define INFINITY (1001)

using namespace std;

int main(void)
{
	while (1){
	int time[300][300];
	int cost[300][300];
	int req[3][100];
	int n, m;
	
	cin >> n >> m;
	if (n == 0 && m == 0){
		break;
	}
	for (int i = 0; i < m; i++){
		for (int j = 0; j < m; j++){
			time[i][j] = cost[i][j] = INFINITY;
		}
	}
	for (int i = 0; i < n; i++){
		int a, b, t, c;
		cin >> a >> b >> c >> t;
		time[a - 1][b - 1] = time[b - 1][a - 1] = t;
		cost[a - 1][b - 1] = cost[b - 1][a - 1] = c;
	}
	
	for (int k = 0; k < m; k++){
		for (int i = 0; i < m; i++){
			for (int j = 0; j < m; j++){
				time[i][j] = min(time[i][j], time[i][k] + time[k][j]);
				cost[i][j] = min(cost[i][j], cost[i][k] + cost[k][j]);
			}
		}
	}
	
	
	int req_count;
	cin >> req_count;
	for (int i = 0; i < req_count; i++){
		cin >> req[0][i] >> req[1][i] >> req[2][i];
	}
	
	for (int i = 0; i < req_count; i++){
		if (req[2][i] == 0){
			cout << cost[req[0][i] - 1][req[1][i] - 1] << endl;
		}
		else {
			cout << time[req[0][i] - 1][req[1][i] - 1] << endl;
		}
	}
	}
	return (0);
}