#include<cstdio>
#include<iostream>
#include<vector>
#include<algorithm>
using namespace std;
#define INF 1000000000
struct data{
	int cost,time;
};
int main(){
	int n,m;
	while(cin >> n >> m,m+n){
		data d[110][110];
		for(int i = 0; i < 110; i++){
			for(int j = 0; j < 110; j++){
				d[i][j].cost = INF;
				d[i][j].time = INF;
			}
		}
		for(int i = 1; i <= n; i++){
			int a,b,c,t;
			cin >> a >> b >> c >> t;
			d[a][b].cost = c;
			d[a][b].time = t;
			d[b][a].cost = c;
			d[b][a].time = t;
		}
		for(int k = 1; k <= m; k++){
			for(int i = 1; i <= m; i++){
				for(int j = 1; j <= m; j++){
					d[i][j].cost = min(d[i][j].cost,d[i][k].cost+d[k][j].cost);
					d[i][j].time= min(d[i][j].time,d[i][k].time+d[k][j].time);
				}
			}
		}
		int k;
		int p,q,r;
		cin >> k;
		for(int i = 0; i < k; i++){
			cin >> p >> q >> r;
			if(r == 0)cout << d[p][q].cost << endl;
			else cout << d[p][q].time << endl;
		}
	}
	return 0;
}