#include <iostream>
#include <vector>
#include <algorithm>
#include <climits>

using namespace std;

typedef vector< int > VI;
typedef vector< VI > VVI;

int main(){
	while(true){
		int n, m;

		cin >> n >> m;

		if(n == 0 && m == 0){
			break;
		}

		VVI map_cost(m, VI(m, INT_MAX / 4));
		VVI map_time(m, VI(m, INT_MAX / 4));

		for(int i = 0; i < m; i++){
			map_cost[i][i] = 0;
			map_time[i][i] = 0;
		}

		for(int i = 0; i < n; i++){
			int a, b, cost, time;
			cin >> a >> b >> cost >> time;

			a--;
			b--;

			map_cost[a][b] = cost;
			map_time[a][b] = time;
			map_cost[b][a] = cost;
			map_time[b][a] = time;
		}

		for(int k = 0; k < m; k++){
			for(int i = 0; i < m; i++){
				for(int j = 0; j < m; j++){
					map_cost[i][j] = min(map_cost[i][j], map_cost[i][k] + map_cost[k][j]);
					map_time[i][j] = min(map_time[i][j], map_time[i][k] + map_time[k][j]);
				}
			}
		}

		int k;
		cin >> k;

		int p, q, r;

		for(int i = 0; i < k; i++){

			cin >> p >> q >> r;

			p--;
			q--;

			if(r == 0){
				cout << map_cost[p][q] << endl;
			}
			else if(r == 1){
				cout << map_time[p][q] << endl;
			}
		}
	}
}