#include<bits/stdc++.h>
using namespace std;
typedef pair<int,int> P;
struct Graph{
  Graph(int n):E(n),d(n){}
  vector<vector<tuple<int,int,int>>>E;
  vector<int>d;
  void add_edge(int a,int b,int c,int d){
    E[a].push_back(make_tuple(b,c,d));
  }
  void dijkstra(int s,int f){
    priority_queue<P,vector<P>,greater<P>>q;
    fill(d.begin(),d.end(),1e9);
    q.push({d[s]=0,s});
    while(q.size()){
      auto p=q.top();q.pop();
      if(p.first>d[p.second])continue;
      for(auto& a:E[p.second]){
        int x=get<0>(a),y=get<1>(a)+p.first;
        if(f)y=get<2>(a)+p.first;
        if(y<d[x])d[x]=y,q.push({y,x});
      }
    }
  }
};
main(){
  int n,m,a,b,c,d,k;
  while(cin>>n>>m,n){
    Graph E(m);
    for(int i=0;i++<n;E.add_edge(--a,--b,c,d),E.add_edge(b,a,c,d))cin>>a>>b>>c>>d;
    cin>>k;
    for(int i=0;i++<k;cout<<E.d[--b]<<endl){
      cin>>a>>b>>c;
      E.dijkstra(--a,c);
    }
  }
}
#include<iostream>
#include<string.h>
#include<cstdio>
#include<algorithm>
#include<stack>
#include<queue>
#include<vector>
#include<cmath>
#include<utility>
#define ll long long int
#define ld long double
#define INF 1000000000
#define EPS 0.0000000001
#define rep(i,n) for(i=1;i<=n;i++)
using namespace std;
typedef pair<int, int> P;

int c[100][100]={};
int t[100][100]={};

int main()
{
  int n,m,a,b;
  int i,j,k;
  int p,q,r;

  while(1){
    cin>>n>>m;
    if(n==0 && m==0)break;

    rep(i,m)rep(j,m)c[i][j]=t[i][j]=INF;//INFテ」ツ?ァテ・ツ按敕ヲツ慊淌・ツ個?
    rep(i,m)c[i][i]=t[i][i]=0;//テ・ツ青古ゥツァツ?」ツ?ッ0

    rep(i,n){
      cin>>a>>b;
      cin>>c[a][b]>>t[a][b];
      c[b][a]=c[a][b];
      t[b][a]=t[a][b];
    }
    
    rep(k,m)rep(i,m)rep(j,m){
      c[i][j]=min(c[i][j],c[i][k]+c[k][j]);
      t[i][j]=min(t[i][j],t[i][k]+t[k][j]);
    }

    cin>>k;
    rep(i,k){
      cin>>p>>q>>r;
      if(r)
	cout<<t[p][q]<<endl;
      else
	cout<<c[p][q]<<endl;
    }
  }
}