#include <cstdio>
#include <vector>

using namespace std;

const int INF = 99999999;

class Edge{
public:
  int from,to,cost,time;
  void swap(){
    int self = from;
    from = to;
    to = self;
  }
  int getvalue(int r){
    if(r == 0){
      return cost;
    }else{
      return time;
    }
  }
};

vector<Edge> edge;
int n,m;

int solve(const int p,const int q,const int r);

int main(){
  edge.clear();
  while(1){
    edge.clear();
    scanf("%d %d",&n,&m);
    if(n==0 && m==0){ break; }
    for(int i=0;i<n;i++){
      Edge ed;
      scanf("%d %d %d %d",&ed.from,&ed.to,&ed.cost,&ed.time);
      edge.push_back(ed);
      ed.swap();
      edge.push_back(ed);
    }
    
    int k;
    scanf("%d",&k);
    for(int i=0;i<k;i++){
      int p,q,r;
      scanf("%d %d",&p,&q);
      if(p==0 && q==0){
        return 0;
      }
      scanf("%d",&r);
      printf("%d\n",solve(p,q,r));
    }
  }
  return 0;
}

int solve(const int p,const int q,const int r){
  int *a = new int[m+1];
  for(int i=0;i<=m;i++){
    a[i] = INF;
  }
  a[p] = 0;
  bool update;
  while(true){
    update = false;
    vector<Edge>::iterator it = edge.begin();
    while(it != edge.end()){
      if(a[(*it).from] != INF && a[(*it).to] > a[(*it).from] + (*it).getvalue(r)){
        a[(*it).to] = a[(*it).from] + (*it).getvalue(r);
        update = true;
      }
      it++;
    }
    if(!update){
      break;
    }
  }
  
  int ans = a[q];
  return a[q];
}