#include <iostream>
#include <algorithm>
using namespace std;
const int INF = 1000000;
int cost[301][301],ttime[301][301];
int main(){
	int n,m;
	while(cin >> n >> m,n || m){
		int a,b,icost,itime,c,r;
		for(int i = 0;i <= m;++i){
			for(int j = 0;j <= m;++j){
				if(i == j) {
					cost[i][i] = 0;
					ttime[i][i] = 0;
				}
				else {
					cost[i][j] = INF;
					ttime[i][j] = INF;
				}
			}
		}
		for(int i = 0;i < n;++i){
			cin >> a >> b >> icost >> itime;
			cost[a][b] = cost[b][a] = icost;
			ttime[a][b] = ttime[b][a] = itime;
		}
		cin >> c;
		for(int d = 0;d < c;++d){
			cin >> a >> b >> r;
			for(int k = 0;k <= m;++k){
				for(int i = 0;i <= m;++i){
					for(int j = 0;j <= m;++j){
						if(r == 1) ttime[i][j] = min(ttime[i][j],ttime[i][k] + ttime[k][j]);
						else cost[i][j] = min(cost[i][j],cost[i][k] + cost[k][j]);
					}
				}
			}
			if(r == 1) cout << ttime[a][b] << endl;
			else cout << cost[a][b] << endl;
		}
	}
	return 0;
}