#include <iostream>
#include <algorithm>

#define N 101
#define inf 1000000

using namespace std;

int main()
{
  int n, m, a, b, p, q, r, cs, tm;
  int cost[N][N], time[N][N];

  while(cin>>n>>m && n+m){
    fill(&cost[0][0], &cost[m][m], inf);
    fill(&time[0][0], &time[m][m], inf);

    for(int i = 1; i <= m; ++i){
      cost[i][i] = 0;
      time[i][i] = 0;
    }

    while(n--){
      cin >> a >> b >> cs >> tm;
      cost[a][b] = cost[b][a] = cs;
      time[a][b] = time[b][a] = tm;
    }
    
    // warshall-floyd
    for(int k = 1; k <= m; ++k)
      for(int i = 1; i <= m; ++i)
	for(int j = 1; j <= m; ++j){
	  cost[i][j] = min(cost[i][j], cost[i][k]+cost[k][j]);
	  time[i][j] = min(time[i][j], time[i][k]+time[k][j]);
	}

    cin >> n;
    while(n--){
      cin >> p >> q >> r;
      cout << ((r)? time[p][q]: cost[p][q]) << endl;
    }
  }

  return 0;
}