#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <map>
#include <utility>
#include <set>
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <sstream>
#include <queue>
#include <cstring>
#include <functional>

using namespace std;

#define REP(i,a,b) for((i)=(a);(i)<(int)(b);(i)++)
#define rep(i,n) REP(i,0,n)
#define foreach(itr,c) for(__typeof((c).begin()) itr=(c).begin();itr!=(c).end();itr++)

typedef pair<int, int> P;
const int INF = 1<<29;

int dijkstra(int s, int g, vector<vector<P> > & G){
  int i;
  int m = G.size();
  vector<int> d(m,INF);
  priority_queue<P,vector<P>,greater<P> > q;

  d[s] = 0;
  q.push(P(0,s));
  while(!q.empty()){
    P p = q.top(); q.pop();
    if(d[p.second] < p.first) continue;
    if(p.second == g) return d[g];

    rep(i,G[p.second].size()){
      P e = G[p.second][i];
      if(d[e.first] > d[p.second] + e.second){
        d[e.first] = d[p.second] + e.second;
        q.push(P(d[e.first], e.first));
      }
    }
  }
  return INF;
}

int main(){
  int n,m,k;
  vector<vector<P> > times, costs;

  while(scanf("%d %d", &n,&m)){
    if(n == 0 && m == 0) break;

    times.resize(m);
    costs.resize(m);
    while(n--){
      int from, to, cost, time;
      scanf("%d %d %d %d",&from,&to,&cost,&time);
      from--; to--;
      times[from].push_back(P(to,time));
      costs[from].push_back(P(to,cost));
    }

    scanf("%d",&k);
    while(k--){
      int s, g, bit;
      scanf("%d %d %d",&s,&g,&bit);
      s--; g--;

      if(bit == 0) printf("%d\n",dijkstra(s,g,costs));
      if(bit == 1) printf("%d\n",dijkstra(s,g,times));
    }
  }

  return 0;
}