#include <iostream>
#include <algorithm>
using namespace std;
const int MAX=100;
const int INF=(1<<21);
int main(){
  int road1[MAX][MAX];
  int road2[MAX][MAX];
  int n,m;
  int a,b,cost,time;
  int p,q,r;
  int x;
  while(1){
    cin >> n >> m;
    for(int i=0;i<n;i++){
      for(int j=0;j<m;j++){
	road1[i][j]=INF;
	road2[i][j]=INF;
      }
    }
    if(n==0 && m==0)break;
    for(int i=0;i<n;i++){
      cin >> a >> b >> cost >> time;
      road1[a-1][b-1]=road1[b-1][a-1]=cost;
      road2[a-1][b-1]=road2[b-1][a-1]=time;
    }

    for(int k=0;k<n;k++){
      for(int i=0;i<n;i++){
	for(int j=0;j<n;j++){
	  road1[i][j]=min(road1[i][j],road1[i][k]+road1[k][j]);
	  road2[i][j]=min(road2[i][j],road2[i][k]+road2[k][j]);
	}
      }
    }
    
    cin >> x;
    for(int i=0;i<x;i++){
      cin >> p >> q >> r;
      if(r==0){
	cout << road1[p-1][q-1] << endl;
      }
      else{
	cout << road2[p-1][q-1] << endl;
      }
    }
  }
}
	