#include<iostream>
#include<string.h>
using namespace std;
#define WHITE 0
#define GLAY 1
#define BLACK 2
#define INF 9999
int kingaku(void);
int saitan(void);
int n,m,p,q;
int cost[101][101],timev[101][101];
int main()
{
	int a,b,c,t,k,r;
	while(1){
		memset(cost,INF,sizeof(cost));
		memset(timev,INF,sizeof(timev));
		cin >> n >> m;
		if(n==0 && m==0) break;
		for(int i=0;i<n;i++){
			cin >> a >> b >> c >> t;
			cost[a][b] = cost[b][a] = c;
			timev[a][b] = timev[b][a] = t;
		}
		cin >> k;
		for(int i=0;i<k;i++){
			cin >> p >> q >> r;
			if(r==0){
				cout << kingaku() << endl;
			}
			else if(r==1)
				cout << saitan() << endl;
		}
	}
	return 0;
}

int kingaku()
{
	int minv;
	int dd[101],color[101];

	for(int i=0;i<=n;i++){
		dd[i] = INF;
		color[i] = WHITE;
	}
	dd[p] = 0;
	color[p] = GLAY;
	while(1){
		minv = INF;
		int u = -1;
		for(int i=1;i<=m;i++){
			if(minv > dd[i] && color[i] != BLACK ){
				u = i;
				minv = dd[i];
			}
		}
		if(u == -1) break;
		color[u] = BLACK;
		for(int v=1;v<=m ;v++){
			if(color[v] != BLACK && cost[u][v] != INF){
				if(dd[v] > dd[u] + cost[u][v]){	
					dd[v] = dd[u] + cost[u][v];
				}
			}
		}
	}
	//for(int i=0;i<n;i++) cout << dd[i] << endl;
	return dd[q];
}

int saitan()
{
	int minv;
	int dd[101],color[101];

	for(int i=0;i<=n;i++){
		dd[i] = INF;
		color[i] = WHITE;
	}
	dd[p] = 0;
	color[p] = GLAY;
	while(1){
		minv = INF;
		int u = -1;
		for(int i=1;i<=m;i++){
			if(minv > dd[i] && color[i] != BLACK ){
				u = i;
				minv = dd[i];
			}
		}
		if(u == -1) break;
		color[u] = BLACK;
		for(int v=1;v<=m;v++){
			if(color[v] != BLACK && timev[u][v] != INF){
				if(dd[v] > dd[u] + timev[u][v]){	//d[v]:??????
					dd[v] = dd[u] + timev[u][v];
				}
			}
		}
	}
	//for(int i=0;i<n;i++) cout << dd[i] << endl;
	return dd[q];
}