//2013/11/15(Fri)

#include <iostream>
using namespace std;
int main()
{
	int n,m;
	int a,b,cost[3000][3000],time[3000][3000];
	int k;
	int p,q,r;
	int INF = 1001;

	int dcost[3000][3000],dtime[3000][3000];
	while(true){
		cin >> n >> m;
		if(n==0 && m==0)
			break;

		for(int i=0; i<n; i++)
			for(int j=0; j<n; j++){
				if(i==j){
					cost[i][j] = 0;
					time[i][j] = 0;
				}		
				else{
					cost[i][j] = INF;
					time[i][j] = INF;
				}
			}

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

			
		for(int i=0; i<n; i++)
			for(int j=0; j<n; j++){
				dcost[i][j] = cost[i][j];
				dtime[i][j] = time[i][j];
			}
		for(int h=0; h<n; h++)
			for(int i=0; i<n; i++)
				for(int j=0; j<n; j++){
					if(dcost[i][j] > dcost[i][h] + dcost[h][j])
						dcost[i][j] = dcost[i][h] + dcost[h][j];
					if(dtime[i][j] > dtime[i][h] + dtime[h][j])
						dtime[i][j] = dtime[i][h] + dtime[h][j];
				}

		cin >> k;
		for(int i=0; i<k; i++){
			cin >> p >> q >> r;
			if(r == 0)
				cout << dcost[p][q] << endl;
			else if(r == 1)
				cout << dtime[p][q] << endl;
		}
	}
	return 0;
}