#include<iostream>
#include<vector>

using namespace std;

int i, j, k, p, q, r, n, m, a, b, c, cost[3001], t, dc[200][200], dt[200][200];

int dic(int d[200][200]) {
  cost[p] = 0;
  int min, f[200] = {};
  for(i = 0; i < m; i++) {
    f[p] = 1;
    for(j = 1; j <= m; j++) {
      if(f[j]) continue;
      if(cost[j] > cost[p] + d[p][j]) cost[j]  = cost[p] + d[p][j];
    }
    min = 200000000;
    for(j = 1; j <= m; j++ ) {
      if(f[j]) continue;
      if(min > cost[j]) min = cost[p = j];
    }
  }
  return cost[q];
}


int main() {
  for(i = 0; i < 200; i++)
    for(j = 0; j < 200; j++) {
      dc[i][j] = 100000000;
      dt[i][j] = 100000000;
    }
  
  cin >> n >> m;
  
  while(n--) {
    cin >> a >> b >> c >> t;
    dc[a][b] = dc[b][a] = c;
    dt[a][b] = dt[b][a] = t;
  }
  
  cin >> k;
  
  while(cin >> p  >> q, p != 0 || q != 0 ) {
    cin >> r;
    for(i = 0; i <= 3000; i++) cost[i] = 1000000000;
    if(r) cout << dic(dt) << endl;
    else cout << dic(dc) << endl;
  }
  
  return 0;
}