#include<iostream>
#include<algorithm>
#include<climits>
using namespace std;
const int INF = INT_MAX/3;
const int MAXN = 2000;
const int TIME = 1,COST = 0;
int graph[2][MAXN][MAXN];
int n,m,a,b,c,t;
void solve();
void init(){
  for(int i = 0;i < m;i++)
    for(int j = 0;j < m;j++)
      graph[0][i][j] =graph[0][j][i] = graph[1][j][i] = graph[1][i][j] = INF;
}
int main(){
  cin >> n >> m;
  init();
  for(int i = 0;i < n;i++){
    cin >> a >> b >> c >> t;
    graph[TIME][a-1][b-1] = graph[TIME][b-1][a-1] = t;
    graph[COST][a-1][b-1] = graph[TIME][b-1][a-1] = c;
  }

  solve();
  
  int X;
  cin >> X;
  for(int i = 0;i < X;i++){
    cin >> a >> b >> c;
    cout << graph[c][a-1][b-1] << endl;
  }
  return 0;
}

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