#include<iostream>
#include<queue>
#include<vector>
#include<functional>

using namespace std;

struct Edge{
	int to,cost;
	Edge(){}
	Edge(int to,int cost):to(to),cost(cost){}
};

int dijkstra(const vector< vector<Edge> > vve,int s,int t){
	priority_queue<pair<int,int>, vector< pair<int,int> >, greater< pair<int,int> > > que;
	pair<int,int> pii;
	pii.first = 0;
	pii.second = s;
	que.push(pii);

	vector<int> dist(vve.size(), INT_MAX);
	
	while(!que.empty()){
		int cost = que.top().first;
		int now = que.top().second;
		que.pop();

		if(dist[now] <= cost){
			continue;
		}

		dist[now] = cost;

		for(int i=0;i<vve[now].size();i++){
			que.push( make_pair(vve[now][i].cost + cost, vve[now][i].to) );
		}
	}

	return dist[t];
}

int main(){
	int n,m;
	while(true){
		cin >> n >> m;
		if(n == 0 && m==0){
			break;
		}

		vector< vector<Edge> > coin(m);
		vector< vector<Edge> > time(m);

		for(int i=0;i<n;i++){
			int a,b,c,t;
			cin >> a >> b >> c >> t;
			coin[a-1].push_back(Edge(b-1,c));
			coin[b-1].push_back(Edge(a-1,c));
			time[a-1].push_back(Edge(b-1,t));
			time[b-1].push_back(Edge(a-1,t));
		}

		int k;
		cin >> k;
		for(int i=0;i<k;i++){
			int p,q,r;
			cin >> p >> q >> r;
			int ans;
			if(r){
				ans = dijkstra(time,p-1,q-1);
			}else{
				ans = dijkstra(coin,p-1,q-1);
			}
			cout << ans << endl;
		}
	}
}