#include<cstdio>
#include<iostream>
#include<cmath>
#include<queue>

#define NMAX 3000
#define MMAX 100
#define TMAX 1000
#define CMAX 1000
#define KMAX 200

using namespace std;

int main(){
	std::ios_base::sync_with_stdio(false);

	int n,m;
	int a,b,time,cost;
	int data[MMAX][MMAX][2];
	int k;
	int p,q,r;
	int dp[MMAX];
	queue<int> now;

	while(1){
		for(int i=0;i<MMAX;i++){
			for(int j=0;j<MMAX;j++){
				for(int k=0;k<2;k++){
					data[i][j][k]=-1;
				}
			}
		}

		for(int i=0;i<MMAX;i++){
			dp[i]=-1;
		}

		cin>>n>>m;
		if(n==0&&m==0) break;
		for(int i=0;i<n;i++){
			cin>>a>>b>>cost>>time;
			data[a][b][1]=time;
			data[b][a][1]=time;
			data[a][b][0]=cost;
			data[b][a][0]=cost;
		}

		cin>>k;
		for(int i=0;i<k;i++){
			cin>>p>>q>>r;
			now.push(p);
			dp[p]=0;
			while(!now.empty()){
				for(int i=0;i<n;i++){
					if(data[now.front()][i][r]!=-1){
						if(dp[i]==-1||dp[i]>dp[now.front()]+data[now.front()][i][r]){
							dp[i]=dp[now.front()]+data[now.front()][i][r];
							now.push(i);
						}
					}
				}
				now.pop();
			}
			cout<<dp[q]<<endl;
		}

	}



	return 0;
}