#include <iostream>
#include <algorithm>
#define INF 10000000
using namespace std;
int cost[101][101];
int t_cost[101][101];
int main(){
  int n,m;
  while(cin >> n >> m , n+m ){
    m++;
    for(int i = 0  ; i < m ; i++ ){
      for(int j = 0  ; j < m ; j++ ){
	if(i == j)cost[i][j] = 0;
	else cost[i][j] = INF; 
	if(i == j)t_cost[i][j] = 0;
	else t_cost[i][j] = INF; 
      }
    }
    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;  
    }
    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]);
	}
      }
    }
    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 {
	cout << t_cost[from][to] << endl;
      }
    }
  }
  return 0;
}