#include<stdio.h>
#define INF 1000000
typedef struct{
	short to,cost,time;
}edge;
int n,m;
int i,j,k;
int p,q,r;
edge e[100][3000];
int ce[100];
bool used[100];
int d[100];

int min(int a,int b){return a<b?a:b;}

int solve(int p,int q,int r){
	int l;
	for(l=0;l<m;l++){
		used[l]=false;
		d[l]=INF;
	}
	d[p]=0;
	int next;
	while(1){
		next=-1;
		for(l=0;l<m;l++){
			if(!used[l]&&(next==-1||d[next]>d[l]))next=l;
		}
		if(next==-1)break;
		used[next]=true;
		for(l=0;l<ce[next];l++){
			if(r==0)d[e[next][l].to]=min(d[e[next][l].to],d[next]+e[next][l].cost);
			if(r==1)d[e[next][l].to]=min(d[e[next][l].to],d[next]+e[next][l].time);
		}
	}
	return d[q];
}

int main(){
	while(1){
		scanf("%d %d",&n,&m);
		if(n==0&&m==0)return 0;
		for(i=0;i<m;i++)ce[i]=0;
		for(i=0;i<n;i++){
			int a,b,cost,time;
			scanf("%d %d %d %d",&a,&b,&cost,&time);
			e[a-1][ce[a-1]].to=b-1;
			e[a-1][ce[a-1]].cost=cost;
			e[a-1][ce[a-1]].time=time;
			e[b-1][ce[b-1]].to=a-1;
			e[b-1][ce[b-1]].cost=cost;
			e[b-1][ce[b-1]].time=time;
			ce[a-1]++;
			ce[b-1]++;
		}
		scanf("%d",&k);
		int ans_c=INF,ans_t=INF;
		for(i=0;i<k;i++){
			scanf("%d %d %d",&p,&q,&r);
			if(r==0)ans_c=min(ans_c,solve(p-1,q-1,r));
			if(r==1)ans_t=min(ans_t,solve(p-1,q-1,r));
		}
		printf("%d\n%d\n",ans_c,ans_t);
	}
}