#include<iostream>

using namespace std;

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

int main() {
  while (true) {
    int n, m;
    cin >> n >> m;
    if (n == 0 && m == 0) break;
    int cost[100][100];
    int time[100][100];
    rep (i, 100) rep (j, 100) cost[i][j] = time[i][j] = 1e9;
    rep (i, 100) cost[i][i] = time[i][i] = 0;
    rep (i, n) {
      int a, b, c, t;
      cin >> a >> b >> c >> t;
      --a;
      --b;
      cost[a][b] = min(cost[a][b], c);
      time[a][b] = min(time[a][b], t);
      cost[b][a] = min(cost[b][a], c);
      time[b][a] = min(time[b][a], t);
    }
    rep (k, m) rep (i, m) rep (j, m) {
      cost[i][j] = min(cost[i][j], cost[i][k] + cost[k][j]);
      time[i][j] = min(time[i][j], time[i][k] + time[k][j]);
    }
    int k;
    cin >> k;
    rep (i, k) {
      int p, q, r;
      cin >> p >> q >> r;
      --p;
      --q;
      if (r == 0) cout << cost[p][q] << endl;
      else cout << time[p][q] << endl;
    }
  }
  return 0;
}