#include<cstdio>
#include<iostream>
#include<algorithm>
#include<deque>
#include<queue>

#define INF 1<<30

using namespace std;

class state{
	public:
	int p,e;
	state(){};
	state(int pp,int aa){p=pp;e=aa;}
	bool operator < (const state& b) const{
		return e > b.e;
	}
};

int n;
int C[101][101];
int T[101][101];

int dike(int st,int en,int bl){
	priority_queue<state> aa;
	int visit[101];
	int now[101];
	
	for(int i=0;i<101;i++){
		visit[i]=0;
		now[i]=INF;
	}
	
	aa.push(state(st,0));
	now[st]=0;
	while(1){
		if(aa.empty()==1){
			break;
		}
		state u=aa.top();
		aa.pop();
		
		visit[u.p]=1;
		
		if(u.p==en){
			return u.e;
		}
		
		for(int i=1;i<=n;i++){
			int cost=C[i][u.p];
			if(bl==1){
				cost=T[i][u.p];
			}
			if(cost==INF){
				continue;
			}
			if(cost+u.e<now[i]){
				now[i]=u.e+cost;
				aa.push(state(i,now[i]));
			}
		}
	}
	return 10;
}

int main(){
	int m;
	scanf("%d%d",&m,&n);
	
	for(int i=0;i<=100;i++){
		for(int j=0;j<=100;j++){
			C[i][j]=T[i][j]=INF;
		}
	}
	for(int i=0;i<m;i++){
		int a,b,c,d;
		scanf("%d%d%d%d",&a,&b,&c,&d);
		C[a][b]=C[b][a]=c;
		T[a][b]=T[b][a]=d;
	}
	scanf("%d",&m);
	for(int i=0;i<m;i++){
		int a,b,c;
		scanf("%d%d%d",&a,&b,&c);
		printf("%d\n",dike(a,b,c));
	}
}