#include<iostream>
#include<algorithm>
#include<vector>
#include<map>
#include<queue>
#define INF (1<<30)
#define COST 0
#define TIME 1

using namespace std;

typedef pair<int, int> p;

vector<p> costs[105], times[105];
int dist[105];
void dijkstra(vector<p> *edge, int s){
	fill(dist, dist + 105, INF);
	dist[s] = 0;
	priority_queue<p, vector<p>, greater<p> > q;
	q.push(p(0, s));
	while(!q.empty()){
		p tmp = q.top();q.pop();
		int d = tmp.first, from = tmp.second;
		if(dist[from] < d)continue;		
		for(int i = 0; i < edge[from].size(); i++){
			int to = edge[from][i].first, c = edge[from][i].second;
			if(dist[to] < dist[from] + c)continue;
			dist[to] = dist[from] + c;
			q.push(p(dist[to], to));
		}
	}
}

int main(){
	while(true){
		int n, m;
		cin >> n >> m;
		if(!n && !m)
			break;
		for(int i = 0; i < n; i++){
			int a, b, c, t;
			cin >> a >> b >> c >> t;
			costs[a].push_back(p(b, c));
			costs[b].push_back(p(a, c));
			times[a].push_back(p(b, t));
			times[b].push_back(p(a, t));
		}
		int k;
		cin >> k;
		for(int i = 0; i < k; i++){
			int p, q, r;
			cin >> p >> q >> r;
			if(r == COST){
				dijkstra(costs, p);
			}else{
				dijkstra(times, p);
			}
			cout << dist[q] << endl;
		}
	}
	return 0;
}