#include<iostream>
#include<map>

using namespace std;

int main(){

  int x, y;

  while(cin >> x >> y){
    if(x == 0 && y == 0) break;

    int money[y+1][y+1], time[y+1][y+1];

    for(int i = 0; i <= y; i++){
      for(int j = 0; j <= y; j++){
	money[i][j] = 0;
	time[i][j] = 0;
      }
    }

    int from, to, mon, tim;

    for(int i = 0; i < x; i++){
      cin >> from >> to >> mon >> tim;
      money[from][to] = mon;
      money[to][from] = mon;
      time[from][to] = tim;
      time[to][from] = tim;
    }

    int num, order, weight[y+1];

    cin >> num;

    int come[y];

    for(int i = 0; i < num; i++){
      cin >> from >> to >> order;

      for(int i = 0; i <= y; i++){
	weight[i] = -1;
	come[i] = 0;
      }

      weight[from] = 0;
      come[from] = 1;

      int min = from;

      if(order == 1){//time
	while(1){
	  from = min;
	  come[from] = 1;
	  for(int i = 1; i <= y; i++){
	    if(come[i] == 0 && time[from][i] != 0){
	      
	      if(weight[i] == -1 || weight[i] > weight[from] + time[from][i]){
		weight[i] = weight[from] + time[from][i];
	      }
	      
	    }
	  }
	  min = -1;
	  
	  for(int i = 0; i <= y; i++){
	    if(come[i] == 1) continue;
	    if(min == -1) min = i;
	    else{
	      if(weight[min] == -1 || weight[min] > weight[i] && weight[i] != -1) min = i;
	    }
	  }
	  if(min == -1) break;
	}
	    
      }else{//money
	while(1){
	  from = min;
	  come[from] = 1;
	  for(int i = 1; i <= y; i++){
	    if(come[i] == 0 && money[from][i] != 0){
	      
	      if(weight[i] == -1 || weight[i] > weight[from] + money[from][i]){
		weight[i] = weight[from] + money[from][i];
	      }
	      
	    }
	  }
	  min = -1;
	  
	  for(int i = 0; i <= y; i++){
	    if(come[i] == 1) continue;
	    if(min == -1) min = i;
	    else{
	      if(weight[min] == -1 || weight[min] > weight[i] && weight[i] != -1) min = i;
	    }
	  }
	  if(min == -1) break;
	}
	   
      }

      cout << weight[to] << endl;      
    }
  }

  return 0;
}
      