#include<iostream>
#define INF 100000000
using namespace std;
int main(){
  int n,m;
  while(cin>>n>>m){
    if(n==0&&m==0) break;
    int d0[m+1][m+1],d1[m+1][m+1];
    for(int i=1;i<=m;i++){
      for(int j=1;j<=m;j++){
    d0[i][j] = INF;
    d1[i][j] = INF;
      }
    }
    int a,b,cost,time;
    for(int i=0;i<n;i++){
      cin >> a >> b >> cost >> time;
      d0[a][b] = d0[b][a] = cost;
      d1[a][b] = d1[b][a] = time;
    }
    for(int i=1;i<=m;i++){
      for(int j=1;j<=m;j++){
    for(int k=1;k<=m;k++){
      d0[j][k] = min(d0[j][k],d0[j][i]+d0[i][k]);
      d1[j][k] = min(d1[j][k],d1[j][i]+d1[i][k]);
    }
      }
    }
    int s,p,q,r;
    cin >> s;
    for(int i=0;i<s;i++){
      cin >> p >> q >> r;
      if(p==q) cout << 0 << endl;
      else if(r==0) cout << d0[p][q] << endl;
      else cout << d1[p][q] << endl;
    }
  }
  return 0;
}