#include<iostream>
#include<cstdio>
using namespace std;
int main(){
  int n,m,a,b,c,d,COST[101][101],TIME[101][101];
  while(1){
    for(int i=0;i<101;i++){
      for(int j=0;j<101;j++){
	COST[i][j] = TIME[i][j] = 1000000;
      }
    }

    cin >> n >> m;
    if(n==0&&m==0)break;
    for(int i=0;i<n;i++){
      cin >> a >> b >> c >> d;
      COST[a][b] = COST[b][a] = c;
      TIME[a][b] = TIME[b][a] = d;
    }
    for(int i=1;i<=m;i++){
      for(int j=1;j<=m;j++){
	for(int k=1;k<=m;k++){
	  if(COST[j][k]>COST[j][i]+COST[i][k]){
	    COST[j][k]=COST[j][i]+COST[i][k];
	  }
	  if(TIME[j][k]>TIME[j][i]+TIME[i][k]){
	    TIME[j][k]=TIME[j][i]+TIME[i][k];
	  }
	}
      }
    }
    cin >> m;
    for(int i=0;i<m;i++){
      cin >> a >> b >> c;
      cout << (c==0?COST[a][b]:TIME[a][b]) << endl;
    }
  }
}
			    