#include <iostream>
#include <algorithm>
const int INF = 1000000;
using namespace std;
int map[101][101][2];//a, b, cost, time;
int d[101];//始点からの最短距離
bool used[101];//使われたかのフラグ
int V;//頂点数

int dijkstra(int start, int end, bool flag){
	fill(d, d + V + 1, INF);
	fill(used, used + V + 1, false);
	d[start] = 0;

	while(1){
		int v = -1;
		for(int u=1;u<=V;u++){
			if(!used[u] && (v == -1 || d[u] < d[v]))v = u;
		}

		if(v == -1)break;

		used[v] = true;

		for(int u=1;u<=V;u++){
			d[u] = min(d[u], d[v] + map[v][u][flag]);
		}
	}

	return d[end];
}

int main(){
	int n, m;
	while(cin >> n >> m, n){
		V = m;

		for(int i=0;i<101;i++){
			for(int j=0;j<101;j++){
				map[i][j][0] = (map[i][j][1] = INF);
			}
		}

		for(int i=n;i;i--){
			int a, b, cost, time;
			cin >> a >> b >> cost >> time;
			map[a][b][0] = cost;
			map[a][b][1] = time;
			map[b][a][0] = cost;
			map[b][a][1] = time;
		}

		int k;
		cin >> k;
		while(k--){
			int p, q, r;
			cin >> p >> q >> r;
			cout << dijkstra(p, q, r) << '\n';
		}
	}
}