#include <iostream>
#include <vector>
#include <stack>
#include <queue>
#include <algorithm>
#include <string>
#include <bitset>
#include <map>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <string.h>

#define X first
#define Y second
#define MP make_pair
typedef long long ll;
using namespace std;
const int MAX = 101;

int mindis[101];

struct E{
	E(int from, int to, int cost, int time):from(from), to(to){
		ct[0] = cost;
		ct[1] = time;
	}
	int from, to;
	int ct[2];	//ct[0]がcost、ct[1]がtime
};

int bf(int s, int e, int mode, vector<E> tb){
	mindis[1] = 0;
	while(1){
		bool update = false;
		for(int i = 0; i < tb.size(); i++){
			if(mindis[tb[i].to] > mindis[tb[i].from] + tb[i].ct[mode]){
				mindis[tb[i].to] = mindis[tb[i].from] + tb[i].ct[mode];
				update = true;
			}
		}
		if(!update) break;
	}
	return mindis[e]-mindis[s];
}

int main(){
	int n, m;	//nが線路数、mが駅数
	while(cin >> n >> m && n|m){
		vector<E> tb;
		for(int i = 0; i < n; i++){
			int a, b, cost, time;
			cin >> a >> b >> cost >> time;
			tb.push_back(E(a, b, cost, time));
		}
		int k;
		cin >> k;
		for(int i = 0; i < k; i++){
			fill(mindis, mindis+101, 1<<29);
			int s, e, mode;
			cin >> s >> e >> mode;
			cout << bf(s, e, mode, tb) << endl;
		}
	}
	return 0;
}