#include <iostream>
#include <vector>
#include <algorithm>
#include <bitset>
#include <queue>

using namespace std;
typedef pair<int,int> pii;

struct edge{
  int to;
  int cost;
  int rev;
  bitset<100> flags;
  edge(int t,int c,int r){
    to=t,cost=c,rev=r;
  }
  edge(){}
};

int dijkstra(int s,int t,int N,const vector<vector<edge> > &graph,vector<int> &clusters){
  const int INF=1<<29;
  //int target_cluster=clusters[t];
  priority_queue<pii,vector<pii>,greater<pii> > pq;
  pq.push(pii(0,s));
  vector<int> d(N,INF);
  while(pq.size()){
    pii p=pq.top();pq.pop();
    int ccost=p.first;
    int cnode=p.second;
    if(d[cnode]<ccost)continue;
    for(int i=0;i<(int)graph[cnode].size();i++){
      edge e=graph[cnode][i];
      //if(!e.flags[target_cluster])continue;
      const int ncost=e.cost+ccost;
      const int to=e.to;
      if(d[to]>ncost){
        d[to]=ncost;
        pq.push(pii(ncost,to));
      }
    }
  }
  return d[t];
}

int main(){

  int N,M;
  while(cin>>N>>M&&(N|M)){
    vector<vector<edge> > graph[2];
    for(int i=0;i<2;i++)
      for(int j=0;j<M;j++)
        graph[i].push_back(vector<edge>());
    for(int i=0;i<N;i++){
      int a,b,c,d;
      cin>>a>>b>>c>>d;
      a--;b--;
      //cout<<a<<" "<<b<<endl;
      graph[0][a].push_back(edge(b,c,graph[0][b].size()));
      graph[0][b].push_back(edge(a,c,graph[0][a].size()-1));
      graph[1][a].push_back(edge(b,d,graph[1][b].size()));
      graph[1][b].push_back(edge(a,d,graph[1][a].size()-1));
    }
    vector<int> clusters(N,0);
    int K;
    cin>>K;
    for(int q=0;q<K;q++){
      int p,q,r;
      cin>>p>>q>>r;
      p--;q--;
      if(r==0){
        cout<<dijkstra(p,q,M,graph[0],clusters)<<endl;
      }
      else{
        cout<<dijkstra(p,q,M,graph[1],clusters)<<endl;
      }
    }
  }
  
  return 0;
}