#include<stdio.h>
#include<string.h>
#define min(x,y) (x<y)?(x):(y)
int main(){
	int n,m,k,p,q,r,c[105][105],t[105][105],v[105],u[105],i,a,b,f,x,y;
	while(scanf("%d%d",&n,&m)!=EOF){
		memset(c, 0,sizeof(c));
		memset(t, 0,sizeof(t));
		for(i=0;i<n;i++){
			scanf("%d%d",&p,&q);
			scanf("%d%d",&c[p][q],&t[p][q]);
			c[q][p]=c[p][q];t[q][p]=t[p][q];
		}
		scanf("%d",&k);
		for(i=0;i<k;i++){
			scanf("%d%d%d",&p,&q,&r);
			//memset(v,-1,sizeof(v));
			memset(u,0,sizeof(u));
			for(a=0;a<=m;a++)v[a]=100000000;
			v[p]=0;f=1;
			while(1){
				x=-1;
				for(a=1;a<=m;a++){
					if(u[a]==0 && (x==-1 || v[a]<v[x]))x=a;
				}
				if(x==-1)break;
				u[x]=1;
				for(a=1;a<=m;a++){
					if(r==0 && c[x][a]!=0)v[a]=min(v[a],v[x]+c[x][a]);
					if(r==1 && t[x][a]!=0)v[a]=min(v[a],v[x]+t[x][a]);
				}
			}
			printf("%d\n",v[q]);
		}
	}
	return 0;
}