#include<cstdio>
#include<queue>
#define INF 100000000
using namespace std;
struct Root{
	int a,b;
	int cost,time;
}root[300];
int main(){
	int n,m;
	int k;
	int p,q,r;
	scanf("%d %d",&n,&m);
	while(n!=0&&m!=0){
		
		for(int i=0;i<n;i++){
			scanf("%d %d %d %d",&root[i].a,&root[i].b,&root[i].cost,&root[i].time);
		}
		scanf("%d",&k);

		for(int i=0;i<k;i++){
			scanf("%d %d %d",&p,&q,&r);
			priority_queue<int> pq;
			int station[100];
			for(int i2=0;i2<100;i2++){
				station[i2]=INF;
			}
			station[p-1]=0;
			pq.push(p);
			while(!pq.empty()){
				int point=pq.top();
				pq.pop();
				for(int i2=0;i2<n;i2++){
					
					if(r==0){
						if(point==root[i2].a&&root[i2].cost+station[point-1]<station[root[i2].b-1]){
							pq.push(root[i2].b);
							station[root[i2].b-1]=root[i2].cost+station[point-1];
						}
						if(point==root[i2].b&&root[i2].cost+station[point-1]<station[root[i2].a-1]){
							pq.push(root[i2].a);
							station[root[i2].b-1]=root[i2].cost+station[point-1];
						}
					}

					if(r==1){
						if(point==root[i2].a&&root[i2].time+station[point-1]<station[root[i2].b-1]){
							pq.push(root[i2].b);
							station[root[i2].b-1]=root[i2].time+station[point-1];
						}
						if(point==root[i2].b&&root[i2].time+station[point-1]<station[root[i2].a-1]){
							pq.push(root[i2].a);
							station[root[i2].b-1]=root[i2].time+station[point-1];
						}
					}

				}
			}
			printf("%d\n",station[q-1]);
		}
		scanf("%d %d",&n,&m);
	}
}