#include<iostream>
using namespace std;

const int MAX_N = 3000;
const int MAX_M = 100;
const int INF = 1000000;

int dist[MAX_M + 1][MAX_M + 1], cost[MAX_M + 1][MAX_M + 1];

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

void minify(int m) {
  for (int k = 1; k <= m; k++) {
    for (int i = 1; i <= m; i++) {
      for (int j = 1; j <= m; j++) {
        dist[i][j] = min(dist[i][j], dist[i][k] + dist[k][j]);
        cost[i][j] = min(cost[i][j], cost[i][k] + cost[k][j]);
      }
    }
  }
}

int query(int p, int q, int r) {
  if (r == 0) {
    return cost[p][q];
  } else {
    return dist[p][q];
  }
}

int main() {
  int n, m;
  while(cin >> n >> m and (n != 0 or m != 0)) {
    int a, b, c, d;
    initialize(m);

    for (int i = 0; i < n; i++) {
      cin >> a >> b >> c >> d;
      cost[a][b] = cost[b][a] = c;
      dist[a][b] = dist[b][a] = d;
    }

    minify(m);

    int k, p, q, r;
    cin >> k;

    for (int i = 0; i < k; i++) {
      cin >> p >> q >> r;
      cout << query(p, q, r) << endl;
    }
  }
}