#include <bits/stdc++.h>
#include <utility>

typedef long long ll;
typedef std::pair<int, int> P;
using namespace std;

struct edge{int to,cost;};

int n,m;
vector<edge> c[1005];
vector<edge> t[1005];
int d[1005];

void dt(int s){
	fill(d,d+1005,INT_MAX/2);
	priority_queue<P, vector<P>, greater<P> > que;
	d[s] = 0;	
	que.push(P(0,s));

	while(!que.empty()){
		P p = que.top(); que.pop();
		int v = p.second;
		if(d[v] < p.first) continue;
		for (int i = 0;i < t[v].size();i++){
			edge e = t[v][i];
			if(d[e.to] > d[v] + e.cost){
				d[e.to] = d[v] + e.cost;
				que.push(P(d[e.to],e.to));
			}
		}
	}
}

void dc(int s){
	fill(d,d+1005,INT_MAX/2);
	priority_queue<P, vector<P>, greater<P> > que;
	d[s] = 0;	
	que.push(P(0,s));

	while(!que.empty()){
		P p = que.top(); que.pop();
		int v = p.second;
		if(d[v] < p.first) continue;
		for (int i = 0;i < c[v].size();i++){
			edge e = c[v][i];
			if(d[e.to] > d[v] + e.cost){
				d[e.to] = d[v] + e.cost;
				que.push(P(d[e.to],e.to));
			}
		}
	}
}


int main() {
	cin.tie(0);
	ios::sync_with_stdio(false);

	while(1){
		cin >> n >> m;
		if(n == 0 && m == 0) break;
		for (int i = 0;i < 1005;i++){
			c[i] = vector<edge>(0);
			t[i] = vector<edge>(0);
		}
		int t1,t2,t3,t4;
		for (int i = 0;i < n;i++){
			cin >> t1 >> t2 >> t3 >> t4;
			c[t1].push_back((edge){t2,t3});
			t[t1].push_back((edge){t2,t4});
			c[t2].push_back((edge){t1,t3});
			t[t2].push_back((edge){t1,t4});
		}

		int k;
		cin >> k;

		for (int i = 0;i < k;i++){
			int p,q,r; cin >> p >> q >> r;
			if(r == 1) dt(p);
			else dc(p);
			cout << d[q] << endl;
		}
	}

	return 0;

}

