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

typedef vector<vector<int> > Matrix;

const int INF = 100000000;
Matrix d; // ??°??????????????¢???????´???????2?¬???????????????£??\?????????
Matrix e; // ??°??????????????¢???????´???????2?¬???????????????£??\?????????
          // d[u][v]??????e=(u,v)??????????????????????????¨???????????´??????INF????????????d[i][i]=0???

void warshall_floyd1(int n) { // n???????????°
  for (int i = 0; i < n; i++)      // ?????±????????????
    for (int j = 0; j < n; j++)    // ????§???????
      for (int k = 0; k < n; k++)  // ??????
        d[j][k] = min(d[j][k], d[j][i] + d[i][k]);
}

void warshall_floyd2(int n) { // n???????????°
  for (int i = 0; i < n; i++)      // ?????±????????????
    for (int j = 0; j < n; j++)    // ????§???????
      for (int k = 0; k < n; k++)  // ??????
        e[j][k] = min(e[j][k], e[j][i] + e[i][k]);
}

int main() {
  int n, m;
  cin >> n >> m;

  d = Matrix(m, vector<int>(m, INF));
  e = Matrix(m, vector<int>(m, INF));
  for (int i = 0; i < m; i++) d[i][i] = 0;
  for (int i = 0; i < m; i++) e[i][i] = 0;
  
  for (int i = 0; i < n; i++) {
    int from, to, cost,time;
    cin >> from >> to >> cost >> time;
    d[from-1][to-1] = cost;
    e[from-1][to-1] = time;
  }
  
  warshall_floyd1(m);
  warshall_floyd2(m);
  
  int quiry;
  cin >> quiry;
  
  for(int i= 0; i < quiry; i++){
  	int a,b,c;
  	cin >> a >> b >> c;
  	if(c == 0) cout << d[a-1][b-1] << endl;
  	else cout << e[a-1][b-1] << endl;
  }
  
  
  return 0;
}