#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;

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

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

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

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

  while(scanf("%d %d",&e,&m)){
    if(e == 0 && m == 0) break;
    times.resize(m);
    costs.resize(m);

    while(e--){
      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",&n);
    while(n--){
      int s,g,bit;

      scanf("%d %d %d",&s,&g,&bit);
      s--; g--;

      if(bit == 0){
        vector<int> d = dijkstra(m,s,costs);
        printf("%d\n",d[g]);
      }
      if(bit == 1){
        vector<int> d = dijkstra(m,s,times);
        printf("%d\n",d[g]);
      }
    }
  }

  return 0;
}