#include <iostream>
#include <algorithm>
#define INF 1000000000
using namespace std;

int cost[101][101];
int t_cost[101][101];
int n,m;

void warshall_floyd(){
  for(int i = 0 ; i < m ; i++ ){
    for(int j = 0 ; j < m ; j++ ){
      for(int k = 0 ; k < m ; k++ ){
	cost[i][j] = min(cost[i][j],cost[i][k] + cost[k][j]);
	t_cost[i][j] = min(t_cost[i][j],t_cost[i][k] + t_cost[k][j]);
      }
    }
  }
}

void input(){
  for(int i = 0 ; i < n ; i++ ){
    int a,b,c,d;
    cin >> a >> b >> c >> d;
    cost[a][b] = cost[b][a] = c;
    t_cost[a][b] = t_cost[b][a] = d;  
  }
}

void init(){
  for(int i = 0  ; i < m ; i++ ){
    for(int j = 0  ; j < m ; j++ ){
      if(i == j){
	cost[i][j] = 0;
	t_cost[i][j] = 0;
      }else{
	cost[i][j] = INF; 
	t_cost[i][j] = INF; 
      }
    }
  }
}

int main(){

  while(cin >> n >> m , n+m ){
    m++;
    init();
    input();
    warshall_floyd();
    int k;
    cin >> k;
    for(int i = 0 ; i < k ; i++ ){
      int from,to,r;
      cin >> from >> to >> r; 
      if(r == 0){
	cout << cost[from][to] << endl;
      } else if(r == 1) {
	cout << t_cost[from][to] << endl;
      }
    }
  }
  return 0;
}