#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using pii = pair<int, int>;

const int INF = 1 << 28;
int n, m;
int a[3010], b[3010], c[3010], t[3010];
int dis0[210][210], dis1[210][210];

int main() {
  cin.tie(0);
  ios_base::sync_with_stdio(false);
  cout << fixed << setprecision(10);
  
  while (cin >> n >> m, n) {
    for (int i = 0; i < n; i++) {
      cin >> a[i] >> b[i] >> c[i] >> t[i];
      a[i]--, b[i]--;
    }
    fill_n((int*)dis0, 210 * 210, INF);
    fill_n((int*)dis1, 210 * 210, INF);
    for (int i = 0; i < m; i++) {
      dis0[i][i] = dis1[i][i] = 0;
    }
    for (int i = 0; i < n; i++) {
      dis0[a[i]][b[i]] = dis0[b[i]][a[i]] = c[i];
      dis1[a[i]][b[i]] = dis1[b[i]][a[i]] = t[i];
    }
    for (int k = 0; k < m; k++) {
      for (int i = 0; i < m; i++) {
        for (int j = 0; j < m; j++) {
          dis0[i][j] = min(dis0[i][j], dis0[i][k] + dis0[k][j]);
          dis1[i][j] = min(dis1[i][j], dis1[i][k] + dis1[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 << dis0[p][q] << endl;
      else cout << dis1[p][q] << endl;
    }
  }  

  return 0;
}
