#include<cstdio>
#include<iostream>
#include<vector>
#include<string>
#include<queue>
#include<map>

using namespace std;

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

#define INF 1000000000

int main(){
	while(1){
		int n,m;
		cin>>n>>m;
		
		if(n==0 && m==0)break;
		
		int dp[111][111][2];
		rep(p,2){
		rep(i,111)rep(j,111){
			dp[i][j][p]=INF;
		}
		}
		
		
		rep(i,n){
			int a,b,c,d;
			cin>>a>>b>>c>>d;
			
			dp[a][b][0] = c;
			dp[b][a][0] = c;
			
			dp[a][b][1] = d;
			dp[b][a][1] = d;
		}
		
		for(int p=0;p<2;p++){
		for(int k=1;k<=m;k++){
			for(int i=1;i<=m;i++){
				for(int j=1;j<=m;j++){
					dp[i][j][p] = min(dp[i][j][p], dp[i][k][p]+dp[k][j][p]);
				}
			}
		}
		}
		
		int r;
		cin>>r;
		
		rep(i,r){
			int a,b,c;
			cin>>a>>b>>c;
			printf("%d\n",dp[a][b][c]);
		}
	}
}