#include <iostream>
using namespace std;

int min_route(int matrix[101][101], int n, int a, int b)
{
  int cost[101];
  bool done[101];
  const int MAX = 999999999;

  for (int i=1; i<=n; i++)
  {
    cost[i] = MAX;
    done[i] = false;
  }

  cost[a] = 0;
  while (1)
  {
    int mincost = MAX;
    int min_s;
    for (int i=1; i<=n; i++)
    {
      if ((!done[i]) && (mincost > cost[i]))
      {
        min_s = i;
        mincost = cost[i];
      }
    }
    if (min_s == b) return cost[b];
    for (int i=1; i<=n; i++)
    {
      if ((matrix[min_s][i] > 0) && (cost[i] > mincost + matrix[min_s][i]))
      {
        cost[i] = mincost+matrix[min_s][i];
      }
    }
    done[min_s] = true;
  }
}


int main()
{
  int time[101][101];
  int cost[101][101];
  int n, m;                     // n: ?????±?????°, m: ?§??????°
  int a, b, c, t;

  cin >> n >> m;

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

  for (int i=0; i<n; i++)
  {
    cin >> a >> b >> c >> t;
    time[a][b] = t;
    time[b][a] = t;
    cost[a][b] = c;
    cost[b][a] = c;
  }

  int k;                        // ?????¨????????°
  int p, q, r;                  // r=0:????°??????? 1:????????????
  cin >> k;
  for (int i=1; i<=k; i++)
  {
    cin >> p >> q >> r;
    if (r==0)
    {
      cout << min_route(cost, m, p, q) << endl;
    }
    else
    {
      cout << min_route(time, m, p, q) << endl;
    }
  }
  return 0;
}