#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
typedef pair<int, int> P;

#define pb push_back
#define rep(i, a, n) for(int i = (a); i < (n); i++)
#define dep(i, a, n) for(int i = (a); i >= (n); i--)
#define mod 1e9+7

__attribute__((constructor))
void initial() {
  cin.tie(0);
  ios::sync_with_stdio(false);
}

struct edge {int to, cost, time;};

int main() {
  int n, m;
  while(cin >> n >> m) {
    if(!n && !m) return 0;
    vector<edge> ra[101];

    rep(i, 0, n) {
      int a, b, c, t;
      cin >> a >> b >> c >> t;
      edge f, g;
      f.to = b, g.to = a, f.cost = c, g.cost = c, f.time = t, g.time = t;
      ra[a].pb(f);
      ra[b].pb(g);
    }

    int k;
    cin >> k;
    rep(i, 0, k) {
      int s, e, r;
      cin >> s >> e >> r;
      int si[101];
      priority_queue<P, vector<P>, greater<P> > que;
      fill(si, si + 101, INT_MAX);
      si[s] = 0;
      que.push(P(0, s));
      while(!que.empty()) {
        P p = que.top();
        que.pop();
        int v = p.second;
        if(si[v] < p.first) continue;
        rep(i, 0, ra[v].size()) {
          edge ed = ra[v][i];
          if(si[ed.to] > si[v] + (r ? ed.time : ed.cost)) {
            si[ed.to] = si[v] + (r ? ed.time : ed.cost);
            que.push(P(si[ed.to], ed.to));
          }
        }
      }

      cout << si[e] << endl;
    }
  }
}