#include <iostream>

using namespace std;

const int INF = 1000000;
int C[101][101], T[101][101];

int main() {
  for (;;) {
    int n, m;
    cin >> n >> m;
    if (!n && !m) return 0;
    for (int i = 1; i <= n; i++)
      for (int j = 1; j <= n; j++)
        C[i][j] = T[i][j] = INF;
    for (int i = 0; i < n; i++) {
      int a, b, cost, time;
      cin >> a >> b >> cost >> time;
      C[a][b] = C[b][a] = cost;
      T[a][b] = T[b][a] = time;
    }
    for (int k = 1; k <= n; k++)
      for (int i = 1; i <= n; i++)
        for (int j = 1; j <= n; j++) {
          C[i][j] = min(C[i][j], C[i][k] + C[k][j]);
          T[i][j] = min(T[i][j], T[i][k] + T[k][j]);
        }
    int k;
    cin >> k;
    for (int i = 0; i < k; i++) {
      int p, q, r;
      cin >> p >> q >> r;
      cout << (!r ? C[p][q] : T[p][q]) << endl;
    }
  }
  return 0;
}