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

int cost[2][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[0][i][j] = min(cost[0][i][j],cost[0][i][k] + cost[0][k][j]);
	cost[1][i][j] = min(cost[1][i][j],cost[1][i][k] + cost[1][k][j]);
      }
    }
  }
}

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

void init(){
  for(int i = 0  ; i < m ; i++ ){
    for(int j = 0  ; j < m ; j++ ){
      if(i == j){
	cost[0][i][j] = 0;
	cost[1][i][j] = 0;
      }else{
	cost[0][i][j] = INF; 
	cost[1][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; 
      cout << cost[r][from][to] << endl;
    }
  }
  return 0;
}