#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;

#define INF (1<<29)

int main(){
  int time[101][101], cost[101][101];
  
  int n, m;
  int from, to, Cost, Time;
  while(cin >> n >> m){
    if(n == 0 && m == 0) break;
    for(int i = 0 ; i <= m ; i++){
      for(int j = 0 ; j <= m ; j++){
	if(i == j) time[i][j] = cost[i][j] = 0;
	else time[i][j] = cost[i][j] = INF;
      }
    }
    
    while(n--){
      cin >> from >> to >> Cost >> Time;
      cost[from][to] = Cost;
      time[from][to] = Time;
    }
    
    for(int k = 1 ; k <= m ; k++){
      for(int i = 1 ; i <= m ; i++){
	for(int j = 1 ; j <= m ; j++){
	  cost[i][j] = min(cost[i][j], cost[i][k]+cost[k][j]);
	  time[i][j] = min(time[i][j], time[i][k]+time[k][j]);
	}
      }
    }
    int num;
    cin >> n;
    while(n--){
      cin >> from >> to >> num;
      if(num == 0) cout << cost[from][to] << endl;
      else cout << time[from][to] << endl;
    }
  }
  return 0;
}