#include<cstdio>
#include<iostream>
#include<cstring>
#include<algorithm>
using namespace std;
#define INF 9999999
int dijkstra(int x,int y);
int D[128][128];/*Adjacency matrix*/
int n,m;
int main(){
	while(1){
		scanf("%d%d",&n,&m);
		if(n == 0 && m == 0)break;
		memset(D,0,sizeof(D));
		for(int i = 1; i <= n; i++){
			for(int j = 1; j <= n; j++){
				if(i == j)continue;
				D[i][j] = INF;
			}
		}
		
		for(int i = 0; i < n; i++){
			int from,to,cst,tm;
			scanf("%d%d%d%d",&from,&to,&cst,&tm);
			D[from][to] = cst;
			D[to][from] = tm;
		}
		int k;
		scanf("%d",&k);
		for(int i = 0; i < k; i++){
			int from,to,x;
			scanf("%d%d%d",&from,&to,&x);
			(x == 0) ? printf("%d\n",dijkstra(from,to)) : printf("%d\n",dijkstra(to,from));
		}
	}
	return 0;
}
int dijkstra(int x,int y){
	int dist[128];
	bool done[128]={false};
	for(int i = 1; i <= m; i++){
		dist[i] = INF;
	}
	
	dist[x] = 0;
	int cost = 0;
	while(1){
		int e = -1;
		for(int i = 1; i <= m; i++){
			if(done[i] == false && (e == -1 || dist[i] < dist[e])){
				e = i;
			}
		}
		if(e == -1)break;
		done[e] = true;
		if(e == y){
			cost = dist[e];
			break;
		}
		
		for(int i = 1; i <= m; i++){
			dist[i] = min(dist[i],dist[e]+D[e][i]);
		}
		
	}
	
	if(cost == INF)return -1;
	else return cost;
}