#include<iostream>

using namespace std;

int main(){
  int n,m,x,y,cost,time,p,q,r;
  while(true){
    cin >> n >> m;
    if(n+m == 0)break;
    int matrix1[n+2][n+2],matrix2[n+2][n+2];
    for(int i=0;i<n+2;i++){
      for(int j=0;j<n+2;j++){
	matrix2[i][j] = matrix1[i][j] = 1000000;
      }
    }
    for(int i=0;i<n;i++){
      cin >> x >> y >> cost >> time;
      matrix1[x][y] = matrix1[y][x] = cost;
      matrix2[y][x] = matrix2[x][y] = time;
    }
    for(int k=1;k<=n;k++){
      for(int i=1;i<=n;i++){
	for(int j=1;j<=n;j++){
	  matrix1[i][j] = min(matrix1[i][j],matrix1[i][k]+matrix1[k][j]);
	  matrix2[i][j] = min(matrix2[i][j],matrix2[i][k]+matrix2[k][j]);

	}
      }
    }


    int k;
    cin >> k;
    for(int i=0;i<k;i++){
      cin >> p >> q >> r;
      if(r == 0){
	cout << matrix1[p][q] << endl;
      }
      else{
	cout << matrix2[p][q] << endl;
      }

    }

  }
  return 0;
}