#include<iostream>
#include<vector>
#include<algorithm>
#define N 100
#define M 1000

using namespace std;

int Dijkstra(vector< vector<int> > data, int s, int g){
  vector<bool> is_reached(data.size(), false);
  vector<int> ans(data.size(), M);
  ans[s] = 0;

  int i, j;
  i = s;
  while(i != g){
    is_reached[i] = true;
    for(j=0; j<data[i].size(); ++j){
      ans[j] = min(ans[j], ans[i] + data[i][j]);
    }
    for(i=-1, j=0; j<is_reached.size(); ++j){
      if(!is_reached[j] && (i < 0 || ans[i] > ans[j])) i = j;
    }
  }
  return ans[g];
}

int main(){
  int n, m, a, b, c, t;
  int p, q, r;

  while(1){
    cin >> n >> m;
    if(n == 0 && m == 0) break;
    vector< vector<int> > cost(m, vector<int>(m, M));
    vector< vector<int> > time(m, vector<int>(m, M));

    for(int i=0; i<n; ++i){
      cin >> a >> b >> c >> t;
      --a;
      --b;
      cost[a][b] = c;
      cost[b][a] = c;
      time[a][b] = t;
      time[b][a] = t;
    }
    cin >> n;
    for(int i=0; i<n; ++i){
      cin >> p >> q >> r;
      --p;
      --q;
      if(r == 0) cout << Dijkstra(cost, p, q) << endl;
      else cout << Dijkstra(time, p, q) << endl;
    }
  }
  return 0;
}