#include<iostream>
#include<iomanip>
#include<math.h>
#include<algorithm>
#include<string>
#include<vector>
#include<queue>
#include<stack>
#include<set>
#include<map>
#define REP(i, N) for(ll i = 0; i < N; ++i)
#define FOR(i, a, b) for(ll i = a; i < b; ++i)
#define ALL(a) (a).begin(),(a).end()
#define pb push_back
#define INF (long long)1000000000
#define MOD 1000000007
using namespace std;
typedef long long ll;
typedef pair<ll, ll> P;
int dx[4] = {1, 0, -1, 0};
int dy[4] = {0, 1, 0, -1};

int main(void) {
	while(true) {
		int n, m;
		cin>>n>>m;
		if(n == 0 && m == 0) break;
		vector<vector<pair<ll, P> > > l(m);
		REP(i, n) {
			int a, b, c, t;
			cin>>a>>b>>c>>t;
			--a;
			--b;
			l[a].pb(pair<ll, P>(b, P(c, t)));
			l[b].pb(pair<ll, P>(a, P(c, t)));
		}
		int k;
		cin>>k;
		REP(i, k) {
			int p, q, r;
			cin>>p>>q>>r;
			--p;
			--q;
			if(r == 0) {
				vector<ll> d(m, INF);
				priority_queue<P> que;
				que.push(P(0, p));
				d[p] = 0;
				while(!que.empty()) {
					P np = que.top();
					que.pop();
					if(d[np.second] != np.first) continue;
					REP(i, l[np.second].size()) {
						if(d[l[np.second][i].first] > d[np.second] + l[np.second][i].second.first) {
							d[l[np.second][i].first] = d[np.second] + l[np.second][i].second.first;
							que.push(P(d[l[np.second][i].first], l[np.second][i].first));
						}
					}
				}
				cout<<d[q]<<endl;
			} else {
				vector<ll> d(m, INF);
				priority_queue<P> que;
				que.push(P(0, p));
				d[p] = 0;
				while(!que.empty()) {
					P np = que.top();
					que.pop();
					if(d[np.second] != np.first) continue;
					REP(i, l[np.second].size()) {
						if(d[l[np.second][i].first] > d[np.second] + l[np.second][i].second.second) {
							d[l[np.second][i].first] = d[np.second] + l[np.second][i].second.second;
							que.push(P(d[l[np.second][i].first], l[np.second][i].first));
						}
					}
				}
				cout<<d[q]<<endl;
			}
		}
	}
}