//2013/11/15(Fri)

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

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

		for(int i=0; i<m; i++)
			for(int j=0; j<m; 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-1][b-1] >> time[a-1][b-1];
			cost[b-1][a-1] = cost[a-1][b-1];
			time[b-1][a-1] = time[a-1][b-1];
		}  

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

		cin >> k;
		for(int i=0; i<k; i++){
			cin >> p >> q >> r;

			if(r == 0)
				cout << cost[p-1][q-1] << endl;
			else if(r == 1)
				cout << time[p-1][q-1] << endl;
		}
	}
	return 0;
}