#include <iostream>
#include <queue>
#include <vector>
using namespace std;

#define rep(i,n)	for(int i = 0; i < n; i++)
#define INF 1 << 24

class State
{
public:
	int to,ct[2];	//cost,time
	State(int to, int cost,int time) :
	to(to){ct[0] = cost, ct[1] = time;}

};

// routes, stations
int n,m;
int d[101];

int dijkstra(vector<State> *vec, int p, int q, int r)
{
	queue<State> que;
	que.push(State(p,0,0));	//first position
	fill(d, d+101, INF);
	d[p] = 0;
	

	while(que.size()){
		State sta = que.front();
		que.pop();

		rep(i, vec[sta.to].size()){
			if(d[vec[sta.to][i].to] > vec[sta.to][i].ct[r] + d[sta.to]){
				d[vec[sta.to][i].to] = vec[sta.to][i].ct[r] + d[sta.to];
				que.push(vec[sta.to][i]);
			}
		}
	}
	return d[q];
}


int main(void)
{
	int a,b,cost,time;
	int k;
	int p,q,r;
	while(cin >> n >> m, n){
		vector<State> *vec = new vector<State>[m+1];

		rep(i,n){
			cin >> a >> b >> cost >> time;
			vec[a].push_back(State(b,cost,time));
			vec[b].push_back(State(a,cost,time));
		}
		cin >> k;
		rep(i,k){
			cin >> p >> q >> r;
			cout << dijkstra(vec, p, q, r) << endl;
		}
	}
}