#include<cstdio>
#include<iostream>
#include<queue>

#define NMAX 3000
#define MMAX 100
#define INF (1000*MMAX+100)*1000
using namespace std;

int main(void){

	int n,m;
	int a[NMAX],b[NMAX],cost[2][NMAX];
	int k;
	int p,q,r;
	int node[MMAX];
	queue<int> now;

	while(1){
		cin>>n>>m;
		if(n==0||m==0) break;
		for(int i=0;i<n;i++){
			cin>>a[i]>>b[i]>>cost[0][i]>>cost[1][i];
		}
		cin>>k;
		for(int i=0;i<k;i++){
			cin>>p>>q>>r;

			for(int i=0;i<MMAX;i++) node[i]=INF;
			now.push(p);
			node[p]=0;

			while(!now.empty()){
				if(node[q]!=INF){
					if(node[now.front()]>=node[q]){
						now.pop();
						continue;
					}
				}
				for(int j=0;j<n;j++){
					if(a[j]==now.front()){
						if(node[b[j]]>node[a[j]]+cost[r][j]){
							now.push(b[j]);
							node[b[j]]=node[a[j]]+cost[r][j];
						}
					}
					if(b[j]==now.front()){
						if(node[a[j]]>node[b[j]]+cost[r][j]){
							now.push(a[j]);
							node[a[j]]=node[b[j]]+cost[r][j];
						}
					}
				}
				now.pop();
			}
			cout<<node[q]<<endl;
		}
	}

	return 0;
}