#include<iostream>
#include<vector>
using namespace std;

const int inf = 1e8;
int m,n;
//n経路の数（枝）　m駅の数（葉）
struct edge{
  int from,to,cost,time;
  edge(int from,int to,int cost,int time): from(from), to(to), cost(cost), time(time){}
};

vector<edge> es;
int shortest_path(int s,int e,int v){
  int d[m];
  for(int i = 0 ; i < n ; i ++)d[i] = inf;
  d[s] = 0 ;
  while(true){
    bool updata = false;
    for(int i = 0 ; i < n ; i ++){
      edge e = es[i];
      if(d[e.from] != inf && d[e.to]> d[e.from] + e.cost && v == 0){
	d[e.to] = d[e.from] + e.cost;
	updata = true;
      }

      if(d[e.from] != inf && d[e.to]> d[e.from] + e.time && v == 1){
	d[e.to] = d[e.from] + e.time;
	updata = true;
      }
    } 
    if(!updata) break;
  }
  return d[e];
}

int main(void){
  while(cin>>n>>m,m+n){
    for(int i = 0 ; i < n ; i ++){
      int s,t,c,ti;
      cin>>s>>t>>c>>ti;
      s--; t--;
      es.push_back(edge(s,t,c,ti));
    }
    int num;
    cin>>num;
    for(int i = 0 ; i < num ;  i++){
      int s,t,v;
      cin>>s>>t>>v;
      s--; t--;
      //cout<<s <<" " << t << " " << v<<" ";
      cout<<shortest_path(s,t,v)<<endl;
    }
  }
  
    
}