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

void dijkstra(int from, int mode);

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

int main()
{
	while (scanf("%d %d", &n, &m) * n != 0){
		fill(&route[0][0], &route[m - 1][m - 1], make_pair(0, 0));
		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][b] = make_pair(cost, time);
			route[b][a] = make_pair(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 (int i = 0; i < m; i++){
		if (route[from][i].first + route[from][i].second == 0){
			continue;
		}
		if (mode == 0 && mini[i] > mini[from] + route[from][i].first){
			mini[i] = mini[from] + route[from][i].first;
			to.push(i);
		}
		else if (mode == 1 && mini[i] > mini[from] + route[from][i].second){
			mini[i] = mini[from] + route[from][i].second;
			to.push(i);
		}
	}
	while (!to.empty()){
		dijkstra(to.front(), mode);
		to.pop();
	}
}