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

#define NONE INT_MAX>>1

vector< vector<int> > WarshallFloyd(vector< vector<int> >A)
{
  for(int k=0;k<A.size();k++)for(int i=0;i<A.size();i++)for(int j=0;j<A.size();j++)
    A[i][j]=min(A[i][j],A[i][k]+A[k][j]);
  return A;
}

int main()
{
  int n,m,a,b,cost,time,k,p,q,r;
  vector< vector<int> >M,N;
  while(cin>>n>>m,n|m){
    M.resize(m);
    N.resize(m);
    for(int i=0;i<m;i++){
      M[i].resize(m,NONE);
      N[i].resize(m,NONE);
    }
    for(int i=0;i<n;i++){
      cin>>a>>b>>cost>>time;
      a--;b--;
      M[a][b]=M[b][a]=cost;
      N[a][b]=N[b][a]=time;
    }
    M=WarshallFloyd(M);
    N=WarshallFloyd(N);
    cin>>k;
    for(int i=0;i<k;i++){
      cin>>p>>q>>r;
      p--;q--;
      if(r==0)cout<<M[p][q]<<endl;
      else cout<<N[p][q]<<endl;
    }
  }
}