#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <functional>
#include <numeric>
#include <stack>
#include <queue>
#include <map>
#include <set>
#include <utility>
#include <sstream>
#include <complex>

using namespace std;

#define FOR(i,a,b) for(long long i=(a);i<(b);i++)
#define REP(i,N) for(long long i=0;i<(N);i++)
#define ALL(s) (s).begin(),(s).end()
#define fi first
#define se second

#define PI acos(-1.0)
#define INF 1000000007

typedef long long ll;
typedef pair<ll, ll> P;
typedef pair<double, double> PD;
typedef pair<string, ll> PS;
typedef vector<ll> V;

struct edge{
	ll to, cost, time;
	edge(ll _to, ll _cost, ll _time) :to(_to), cost(_cost), time(_time){};
};

ll n, m, a, b, COST, TIME, k, s, g, f;

ll mincost[1000], mintime[1000];
vector<edge> G[1000];

void dijkstra0(ll start){
	fill(mincost, mincost + 1000, INF);
	mincost[start] = 0;
	priority_queue<P> que;
	que.push(P(0, start));

	while (!que.empty()){
		P p = que.top(); que.pop();
		ll v = p.second;
		if (mincost[v] < p.first)continue;

		for (int i = 0; i < G[v].size(); i++){
			edge e = G[v][i];
			if (mincost[e.to] > mincost[v] + e.cost){
				mincost[e.to] = mincost[v] + e.cost;
				que.push(P(mincost[e.to], e.to));
			}
		}
	}
}

void dijkstra1(ll start){
	fill(mintime, mintime + 1000, INF);
	mintime[start] = 0;
	priority_queue<P> que;
	que.push(P(0, start));

	while (!que.empty()){
		P p = que.top(); que.pop();
		ll v = p.second;
		if (mincost[v] < p.first)continue;

		for (int i = 0; i < G[v].size(); i++){
			edge e = G[v][i];
			if (mintime[e.to] > mintime[v] + e.time){
				mintime[e.to] = mintime[v] + e.time;
				que.push(P(mintime[e.to], e.to));
			}
		}
	}
}

int main(){
	while (cin >> n >> m&&n + m){
		REP(i, n){
			cin >> a >> b >> COST >> TIME;
			G[a].push_back(edge(b, COST, TIME));
			G[b].push_back(edge(a, COST, TIME));
		}
		cin >> k;
		REP(i, k){
			cin >> s >> g >> f;
			if (f == 0){
				dijkstra0(s);
				cout << mincost[g] << endl;
			}
			else{
				dijkstra1(s);
				cout << mintime[g] << endl;
			}
		}
	}
}