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

#define NONE INT_MAX

int n,m,k,a,b,c,d,p,q,r;
vector< vector<int> >M,N;

int Dijkstra(int start,int goal)
{
  vector<int>d(n,NONE),visited(n,0);
  multimap<int,int>q;

  d[start]=0;
  q.insert(make_pair(0,start));

  while(q.size()){
    int u,v;
    u=q.begin()->second;
    q.erase(q.begin());
    if(visited[u]==true)continue;
    visited[u]=true;
    for(v=0;v<n;v++){
      if(M[u][v]==NONE)continue;
      if(visited[v]==true)continue;
      if(d[v]<=d[u]+M[u][v])continue;
      d[v]=d[u]+M[u][v];
      q.insert(make_pair(d[v],v));
    }
  }
  return d[goal];
}

int Dijkstra2(int start,int goal)
{
  vector<int>d(n,NONE),visited(n,0);
  multimap<int,int>q;

  d[start]=0;
  q.insert(make_pair(0,start));

  while(q.size()){
    int u,v;
    u=q.begin()->second;
    q.erase(q.begin());
    if(visited[u]==true)continue;
    visited[u]=true;
    for(v=0;v<n;v++){
      if(N[u][v]==NONE)continue;
      if(visited[v]==true)continue;
      if(d[v]<=d[u]+N[u][v])continue;
      d[v]=d[u]+N[u][v];
      q.insert(make_pair(d[v],v));
    }
  }
  return d[goal];
}

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