#include <cstdio>
#include <vector>
#include <algorithm>
#include <queue>
using namespace std;

struct station
{
	station(int to, int cost, int time)
	{
		this->to = to;
		this->cost = cost;
		this->time = time;
	}

	int to;
	int cost;
	int time;
};

void dijkstra(int from, int mode);

vector<station> route[1000];
int mini[1000];
int n, m;

int main()
{
	while (scanf("%d %d", &n, &m) * n != 0){
		for (int i = 0; i < m; i++){
			route[i].clear();
		}
		for (int i = 0; i < n; i++){
			int a, b;
			int cost, time;
			
			scanf("%d %d %d %d", &a, &b, &cost, &time);
			a--, b--;
			route[a].push_back(station(b, cost, time));
			route[b].push_back(station(a, cost, time));
		}
		
		int k;
		scanf("%d", &k);
		while (k-- > 0){
			fill(mini, mini + m, 100000000);
			int p, q, r;
			scanf("%d %d %d", &p, &q, &r);
			p--, q--;
			mini[p] = 0;
			dijkstra(p, r);
			printf("%d\n", mini[q]);
		}
	}
	
	return 0;
}

void dijkstra(int from, int mode)
{
	queue<int> to;
	
	for (vector<station>::iterator i = route[from].begin(); i < route[from].end(); i++){
		if (mode == 0 && mini[i->to] > mini[from] + i->cost){
			mini[i->to] = mini[from] + i->cost;
			to.push(i->to);
		}
		else if (mode == 1 && mini[i->to] > mini[from] + i->time){
			mini[i->to] = mini[from] + i->time;
			to.push(i->to);
		}
	}
	while (!to.empty()){
		dijkstra(to.front(), mode);
		to.pop();
	}
}