#include<bits/stdc++.h>
using namespace std;
#define MAX_VAL 200
#define INF 12345678

int V, d[MAX_VAL][MAX_VAL], z[MAX_VAL][MAX_VAL];

void warshall_floyd(int x[MAX_VAL][MAX_VAL]){
	for(int k = 1; k <= V; k++)
		for(int i = 1; i <= V; i++)
			for(int j = 1; j <= V; j++)
				x[i][j] = min(x[i][j],x[i][k]+x[k][j]);
}

int main(){
	int n, k;
	while(true){
		cin>>n>>V;
		
		if(!n&&!V) break;
		
		for(int i = 0; i <= V; i++)
			for(int j = 0; j <= V; j++)d[i][j]=z[i][j] = i == j ? 0 : INF;
		
		for(int i = 1,a,b,cost,time; i <= n; i++){
			cin>>a>>b>>cost>>time;
			d[a][b] = d[b][a] = cost;
			z[a][b] = z[b][a] = time;
		}
		
		warshall_floyd(d);
		warshall_floyd(z);
		
		cin>>k;
		
		for(int i = 1,p,q,r; i <= k; i++){
			cin>>p>>q>>r;
			if(r)cout<<z[p][q]<<endl;
			else cout<<d[p][q]<<endl;
		}
	}
	
	return 0;
}
