#include <iostream>
#include <algorithm>

const int MAX=100;
using namespace std;
int main(){
  int n,m;
  int i,j,k;
  int a,b,co,ti;
  int x;
  int home1[MAX][MAX];
  int home2[MAX][MAX];
  int p,q,r;
  while(1){
    cin >> n >> m;
    if(n==0 && m==0)break;
    for(i=0;i<n;i++){
      for(j=0;j<n;j++){
	home1[i][j]=9999;
	home2[i][j]=9999;
      }
    }
    for(i=0;i<n;i++){
      cin >> a >> b >> co >> ti;
      home1[a-1][b-1]=home1[b-1][a-1]=co;
      home2[a-1][b-1]=home2[b-1][a-1]=ti;
    }
    for(k=0;k<n;k++){
      for(i=0;i<n;i++){
	for(j=0;j<n;j++){
	  home1[i][j]=min(home1[i][j],home1[i][k]+home1[k][j]);
	  home2[i][j]=min(home2[i][j],home2[i][k]+home2[k][j]);
	}
      }
    }
    cin >> x;
    for(int l=0;l<x;l++){
      cin >> p >> q >> r;
      if(r==0){
	cout << home1[p-1][q-1] << endl;
      }else{
	cout << home2[p-1][q-1] << endl;
      }
    }
  }
}
   