#include<iostream>
using namespace std;

const int INF = 10000000;
int d[101][101];

int main(){
  int W_cost[101][101];
  int W_time[101][101];
  int n,m;
  int a,b,cost,tim;
  cin >> n >> m;
  for(int i=1;i<=m;i++){
    for(int j=1;j<=m;j++){
      W_cost[i][j] = INF;
      W_time[i][j] = INF;
    }
  }

  for(int i=1;i<=m;i++){
    W_cost[i][i] = 0;
    W_time[i][i] = 0;
  }

  for(int i=0;i<n;i++){
    cin >> a >> b >> cost >> tim;
    W_cost[a][b] = cost;
    W_time[a][b] = tim;
  }

  int k;
  cin >> k;
  int p,q,r;
  int ans[50];
  int nn=0;
  while(1){
    cin >> p >> q;
    if(p==0&&q==0)
      break;
    cin >> r;
    if(r){
      for(int i=1;i<=m;i++){
        for(int j=1;j<=m;j++){
          d[i][j] = W_time[i][j];
        }
      }
    }
    else{
      for(int i=1;i<=m;i++){
        for(int j=1;j<=m;j++){
          d[i][j] = W_cost[i][j];
        }
      }
    }

    for(int k=1;k<=m;k++){
      for(int i=1;i<=m;i++){
        for(int j=1;j<=m;j++){
          if(d[i][j]>d[i][k]+d[k][j]){
            d[i][j] = d[i][k]+d[k][j];
          }
          if(d[i][j]>d[j][k]+d[k][i]){
            d[i][j] = d[j][k]+d[k][i];
          }
        }
      }
    }
    nn++;
    ans[nn] = d[p][q];

  }
  for(int i=1;i<=k;i++){
    cout << ans[i] << endl;
  }
}

