#include <iostream>
#include <vector>
#include <queue>
using namespace std;
typedef pair<int, int> P;
const int INF = 1 << 28;
int c[101][101];
int t[101][101];
int cost[101][101];
int tim[101][101];

int main(){
  int n, m, K;
  int p, q, r;
  while(cin >> n >> m && (n || m)){
    fill(&cost[0][0], &cost[100][100] + 1, INF);
    fill(&tim[0][0], &tim[100][100] + 1, INF);
    fill(&c[0][0], &c[100][100] + 1, INF);
    fill(&t[0][0], &t[100][100] + 1, INF);
    for(int i = 1; i <= m; i++){
      cost[i][i] = tim[i][i] = 0;
    }
    for(int i = 0; i < n; i++){
      int a, b;
      cin >> a >> b;
      cin >> c[a][b] >> t[a][b];
      c[b][a] = c[a][b];
      t[b][a] = t[a][b];
    }
    for(int k = 1; k <= m; k++){
      for(int i = 1; i <= m; i++){
	for(int j = 1; j <= m; j++){
	  if(cost[i][j] > cost[i][k] + c[k][j]){
	    cost[i][j] = cost[i][k] + c[k][j];
	  }
	  if(tim[i][j] > tim[i][k] + t[k][j]){
	    tim[i][j] = tim[i][k] + t[k][j];
	  }
	}
      }
    }
    cin >> K;    
    for(int i = 0; i < K; i++){
      cin >> p >> q >> r;
      if(!r){
	cout << cost[p][q] << endl;
      }else{
	cout << tim[p][q] << endl;
      }
    }
  }
  return 0;
}
	
    