#include <iostream>

#define INF (1000000000)
#define min(x, y) ((x) < (y) ? (x) : (y))

int main(void) {
  using namespace std;

  int n, m;

  while (cin >> n >> m, n != 0 && m != 0) {
    int cost[100][100];
    int time[100][100];

    for (int i = 0; i < m; i++) {
      for (int j = 0; j < m; j++) {
        cost[i][j] = time[i][j] = i == j ? 0 : INF;
      }
    }

    for (int i = 0; i < n; i++) {
      int a, b, c, t;
      cin >> a >> b >> c >> t;

      a--; b--;

      cost[a][b] = cost[b][a] = min(c, cost[a][b]);
      time[a][b] = time[b][a] = min(t, time[a][b]);
    }

    for (int k = 0; k < m; k++) {
      for (int i = 0; i < m; i++) {
        for (int j = 0; j < m; j++) {
          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;
    for (int i = 0; i < k; i++) {
      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;
}