#include <iostream>
#include <vector>
#include <queue>
#include <utility>
#include <algorithm>
using namespace std;

struct edge{
  int to,cost;
  edge(){}
  edge(int to,int cost):to(to),cost(cost){}
};
typedef pair<int,int> P;
const int INF=11111;

vector<edge> G_cost[3000];
vector<edge> G_time[3000];
int d[101];

int main(void){
  int n,m,a,b,c,t;
  int k,p,q,r;
  priority_queue<P,vector<P>,greater<P> > que;
  while(cin>>n>>m,!(n==0&&m==0)){
    for(int i=0;i<3000;i++){
      G_cost[i].clear();
      G_time[i].clear();
    }
    for(int i=0;i<n;i++){
      cin>>a>>b>>c>>t;
      G_cost[a].push_back(edge(b,c));
      G_cost[b].push_back(edge(a,c));
      G_time[a].push_back(edge(b,t));
      G_time[b].push_back(edge(a,t));
    }
    cin>>k;
    for(int i=0;i<k;i++){
      cin>>p>>q>>r;
      fill(d,d+m+1,INF);
      d[p]=0;
      que.push(P(0,p));
      if(r==0){
	while(!que.empty()){
	  P p=que.top(); que.pop();
	  if(d[p.second]<p.first)continue;
	  for(int j=0;j<G_cost[p.second].size();j++){
	    edge e=G_cost[p.second][j];
	    if(d[e.to]>d[p.second]+e.cost){
	      d[e.to]=d[p.second]+e.cost;
	      que.push(P(d[e.to],e.to));
	    }
	  }
	}
	cout<<d[q]<<endl;
      }
      if(r==1){
	while(!que.empty()){
	  P p=que.top(); que.pop();
	  if(d[p.second]<p.first)continue;
	  for(int j=0;j<G_time[p.second].size();j++){
	    edge e=G_time[p.second][j];
	    if(d[e.to]>d[p.second]+e.cost){
	      d[e.to]=d[p.second]+e.cost;
	      que.push(P(d[e.to],e.to));
	    }
	  }
	}
	cout<<d[q]<<endl;
      }
    }
  }
  return 0;
}