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

const int N = 110;
int timu[N][N];
int cost[N][N];

int main()
{
  int n,m;
  while(cin >> n >> m ,n|m)
  {
    for(int i=0;i<N;++i)
    for(int j=0;j<N;++j)
      timu[i][j] = cost[i][j] = 1<<30;

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

    for(int k=0;k<N;++k)
    for(int i=0;i<N;++i)
    for(int j=0;j<N;++j)
    {
      cost[i][j] = min(cost[i][j], cost[i][k] + cost[k][j]);
      timu[i][j] = min(timu[i][j], timu[i][k] + timu[k][j]);
    }

    int k;
    cin >> k;
    while(k--)
    {
      int p,q,r;
      cin >> p >> q >> r;
      if(r)
        cout << timu[p][q] << endl;
      else
        cout << cost[p][q] << endl;
    }
  }
  return 0;
}