#include<iostream>
#include<algorithm>
#include<cstring>
#define INF 111111111
using namespace std;

int n,m;
int a,b,c,d,cost[1000][1000],tim[1000][1000];
int memo[1000];
int ans[1000],ans2[1000];
int p,q,r;
int k,x,y,z;

void cost_a(){
  ans[x] = 0;

  while(true){
    int s = -1;

    for(int i=1;i<=m;i++){
      if(!memo[i] && (s == -1 || ans[i] < ans[s])) s = i;
    }

    if(s == -1) break;
    memo[s] = 1;

    for(int i=1;i<=m;i++) ans[i] = min(ans[i],ans[s] + cost[i][s]);
  }
}

void time_a(){
  ans2[x] = 0;

  while(true){
    int s = -1;

    for(int i=1;i<=m;i++){
      if(!memo[i] && (s == -1 || ans[i] < ans2[s])) s = i;
    }

    if(s == -1) break;
    memo[s] = 1;

    for(int i=1;i<=m;i++) ans2[i] = min(ans2[i],ans2[s] + tim[i][s]);
  }
}

int main(){

  while(true){
    cin >> n >> m;
    if(!n && !m) break;

    for(int i=0;i<1000;i++){
      for(int j=0;j<1000;j++) cost[i][j] = tim[i][j] = INF;
      ans[i] = ans2[i] = INF;
    }

    for(int i=0;i<n;i++){
      cin >> a >> b >> c >> d;
     
      if(cost[a][b] > c) cost[a][b] = cost[b][a] = c;
      if(tim[a][b] > d) tim[a][b] = tim[b][a] = d;

    }

    cin >> k;

    for(int i=0;i<k;i++){
      cin >> x >> y >> z;

      memset(memo,0,sizeof(memo));

      if(!z){
	cost_a();
	cout << ans[y] << endl;
      }

      else{
	time_a();
	cout << ans2[y] << endl;
      }
    }
  }
}