#include<cmath>
#include<cctype>
#include<cstdlib>
#include<cstdio>
#include<climits>
#include<iostream>
#include<algorithm>
#include<vector>
#include<deque>
#include<string>
#include<queue>
#include<map>
#include<set>
#include<utility>
#include<queue>

using namespace std;
int n,m;
const int MAXN = 1000;
int t[MAXN][MAXN];
int c[MAXN][MAXN];

void solve(){
  for(int k = 0;k < n; ++k){
    for(int i = 0;i < n; ++i){
      for(int j = 0;j < n; ++j){
	t[i][j] = min(t[i][j],t[i][k] + t[k][j]);
	c[i][j] = min(c[i][j],c[i][k] + c[k][j]);
      }
    }
  }
}
const int INF = INT_MAX/3;
void init(){
  for(int i = 0;i < MAXN; ++i){
    for(int j = 0;j < MAXN;j++){
      c[i][j] = t[i][j] = INF;
    }
  }
  for(int i = 0;i < MAXN;i++){
    c[i][i] = t[i][i] = 0;
  }
}


int main(){
  ios::sync_with_stdio(false);

  while(cin >> n >> m && n+m > 0){
    init();
    int a,b,icost,itime;
    for(int i = 0;i < n;i++){
      cin >> a >> b >> icost >> itime;
      t[a][b] = t[b][a] = itime;
      c[a][b] = c[b][a] = icost;
    }
    solve();
    int k;
    cin >> k;
    for(int i = 0;i < k; ++i){
      int p,q,r;
      cin >> p >> q >> r;
      if(r == 1){
	cout << t[p][q] << endl;
      }else{
	cout << c[p][q] << endl;
      }
    }
  }
  return 0;
}