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

class State{
public:
  int v, c;
  State(int _v, int _c)
    :v(_v), c(_c) {}

  bool operator < (const State& st) const{
    return this->c > st.c;
  }
};


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

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

    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; 
      vector<int> d(m, INF);
      --q;
      --p;
      priority_queue<State> req;
      req.push(State(p, 0));
      d[p] = 0; 
	while(! req.empty()){
	  State st = req.top();
	  req.pop();

	  if (st.v == q) {
	    cout << st.c << endl;
	    break;
	  }

	  if (d[st.v] < st.c)
	    continue;

	  for(int i=0; i<m; i++){
	    if(score[r][st.v][i] != 0){
	      if(st.c + score[r][st.v][i] < d[i]){
		d[i] = st.c + score[r][st.v][i];
		req.push(State(i, d[i]));
	      } 
	    }
	  }
	}

    }
  }
    return 0;
}