#include<iostream>
using namespace std;
int line[3001][3001],c[3001][3001],t[3001][3001];
int stop[3001]={0};
int ls[3001]={0}; 
int route_cost(int p,int q){
  int top = -1;
  for(int i=0;i<ls[p];i++){
    //cout << line[p][i] << endl;
    if(stop[line[p][i]]!=0) continue;
    stop[p]++;
    if(q==line[p][i]){
	stop[p] = 0;
	return c[p][q];
   }
int h = route_cost(line[p][i],q)+c[p][line[p][i]];
    if(top<0||top>h){
      top = h+c[p][line[p][i]];
    }
  }
  stop[p] = 0;
  return top;
}
int route_time(int p,int q){
  int top = -1;
  for(int i=0;i<ls[p];i++){
    //cout << "P " << p << endl;
    //cout << line[p][i] << endl;
    if(stop[line[p][i]]!=0) continue;
    stop[p]++;
      if(q==line[p][i]){
	stop[p] = 0;
	 return t[p][q];
      }
int h = route_time(line[p][i],q)+t[p][line[p][i]];
     if(top<0||top>h){
	top = h;
	//cout << 'O' << endl;
      }
  }
  stop[p] = 0;
  return top;
}
int main(){
  int n,m;
  int a,b,cost,time;
  while(cin>>n>>m){
    if(n==0&&m==0) break;
    for(int i=0;i<n;i++){
      cin >> a >> b >> cost >> time;
      line[a][ls[a]] = b;
      ls[a]++;
      line[b][ls[b]] = a;
      ls[b]++;
      c[a][b] = cost;
      c[b][a] = cost;
      t[a][b] = time;
      t[b][a] = time;
      //cout << "line " << line[a][ls[a]-1] << ' ' << line[b][ls[b]-1] << endl; 
    }
    int k;
    cin >> k;
    int p,q,r;
    for(int i=0;i<k;i++){
      cin>>p>>q>>r;
      if(r==0) cout << route_cost(p,q) << endl;
      else if(r==1) cout << route_time(p,q) << endl;
    }
  }
  return 0;
}