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

int main(){
  int n, m, kk;
  const int INF = 1 << 28;

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

    vector<vector<vector<int> > > score(2, vector<vector<int> >(m, vector<int>(m, INF)));
    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;
    }

    for (int k = 0; k < m; ++k) {
      for (int i = 0; i < m; ++i) {
	for (int j = 0; j < m; ++j) {
	  score[0][i][j] = min(score[0][i][j], score[0][i][k]+score[0][k][j]);
	  score[1][i][j] = min(score[1][i][j], score[1][i][k]+score[1][k][j]);
	}
      }
    }

    int p, q, r;

    cin >> kk;
    while(kk--){
      cin >> p >> q >> r; 
      --q;
      --p;
      cout << score[r][p][q] << endl;
    }
  }
    return 0;
}