#include<iostream>
#include<vector>
#include<queue>

using namespace std;

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

struct Edge {
  int to, cost[2];
};

int main() {
  while (true) {
    int n, m;
    cin >> n >> m;
    if (n == 0 && m == 0) break;
    int a, b, c, t;
    vector<Edge> edge[m];
    rep (i, n) {
      cin >> a >> b >> c >> t;
      --a;
      --b;
      Edge e;
      e.to = b;
      e.cost[0] = c;
      e.cost[1] = t;
      edge[a].push_back(e);
      e.to = a;
      edge[b].push_back(e);
    }
    int k;
    cin >> k;
    rep (iii, k) {
      int p, q, r;
      cin >> p >> q >> r;
      --p;
      --q;
      vector<int> dis;
      rep (i, m) dis.push_back(1e9);
      priority_queue<pair<int, int>, vector<pair<int, int> >, greater<pair<int, int> > > que;
      que.push(make_pair(0, p));
      while (!que.empty()) {
	pair<int, int> now = que.top();
	que.pop();
	if (dis[now.second] <= now.first) continue;
	dis[now.second] = now.first;
	rep (i, edge[now.second].size()) {
	  que.push(make_pair(edge[now.second][i].cost[r] + now.first, edge[now.second][i].to));
	}
      }
      cout << dis[q] << endl;
    }
  }
  return 0;
}