#include<cstdio>
#include<algorithm>
#include<iostream>
#include<vector>
#include<set>
#include<queue>

using namespace std;

#define rep(i,n) for(int i=0;i<int(n);i++)
#define reps(i,n) for(int i=1;i<=int(n);i++)

class L{
	public:
	int a,c,t;
	L(int a,int c,int t):a(a),c(c),t(t){}
};

class S{
	public:
	int now;
	int p;
	S(int now,int p):now(now),p(p){}
	bool operator<(const S&a)const{
		return p>a.p;
	}
};

int main(){
	A:;
	
	int n,m;
	cin>>n>>m;
	
	if(n==0)return 0;
	
	vector<L> load[111];
	
	rep(i,n){
		int a,b,c,t;
		cin>>a>>b>>c>>t;
		
		load[a].push_back(L(b,c,t));
		load[b].push_back(L(a,c,t));
	}
	
	
	int k;
	cin>>k;
	
	rep(pp,k){
		int aa,bb,cc;
		cin>>aa>>bb>>cc;
		
		priority_queue<S> que;
		que.push(S(1,0));
		
		int visit[111]={0};
		while(1){
			if(que.empty())break;
			
			S u = que.top();
			que.pop();
			
			//printf("%d %d\n",u.now,u.p);
			
			if(u.now==m){
				printf("%d\n",u.p);
				break;
			}
			
			if(visit[u.now]==1)continue;
			visit[u.now]=1;
			
			rep(i,load[u.now].size()){
				
				L e = load[u.now][i];
				
				if(cc==0){
					que.push(S(e.a, u.p+e.c));
				}else{
					que.push(S(e.a, u.p+e.t));
				}
			}
		}
	}
	
	goto A;
}