#include <iostream>
#include <algorithm>
#include <vector>
#include <queue>
using namespace std;

int main(){
  int n, m, k;
  const int INF = 1 << 30;

  while(cin >> n >> m){
    if((n|m) == 0) break;

    vector<int> d(m, INF);
    vector<vector<vector<int> > > score(2, vector<vector<int> >(m, vector<int>(m, 0)));
    int a, b, cost, time;

    for(int i=0; i<n; i++){
      cin >> a >> b >> cost >> time;
      a--;
      b--;
      score[0][a][b] = score[0][b][a] = cost;
      score[1][a][b] = score[1][b][a] = time;
    }


    int p, q, r;

    cin >> k;
    while(k--){
      cin >> p >> q >> r; 
      --q;
      --p;
      queue<int> req;
      req.push(p);
      d[p] = 0; 
	while(! req.empty()){
	  int v = req.front();
	  req.pop();

	  for(int i=0; i<m; i++){
	    if(score[r][v][i] != 0){
	      if(d[v] + score[r][v][i] < d[i]){
		d[i] = d[v] + score[r][v][i];
		req.push(i);
	      } 
	    }
	  }
	}
	cout << d[q] << endl;
    }
  }
    return 0;
}