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

struct Node{
  int num;

  vector<int> edge_to;
  vector<int> edge_cost;
  vector<int> edge_time;

  bool done;
  int cost;
  int time;
};

int main(){
  for(;;){
    int n,m,a,b,c,t,p,q,r;
    cin>>n>>m;
    if(n==0&&m==0)break;
    vector<Node> node;

    for(int i=1;i<=m;i++){
      Node no;
      no.num=i;
      no.done=false;
      no.cost=-1;
      no.time=-1;
      node.push_back(no);
    }


    while(n--){
      cin>>a>>b>>c>>t;
      node[a-1].edge_to.push_back(b);
      node[b-1].edge_to.push_back(a);
      node[a-1].edge_cost.push_back(c);
      node[b-1].edge_cost.push_back(c);
      node[a-1].edge_time.push_back(t);
      node[b-1].edge_time.push_back(t);
      //cout<<"Node: "<<node[a-1].num<<"edge:"<<endl;
      //cout<<"Node: "<<node[b-1].num<<"edge:"<<endl;
    }

    cin>>n;
    while(n--){
      cin>>p>>q>>r;
      vector<Node> n(node.size());
      copy(node.begin(),node.end(),n.begin());
      p--;
      q--;
      if(r){

	n[p].time = 0;
	//cout<<"n[p].time "<<n[p].time<<endl;

	while(1){

	  Node doneNode;
	  int d=-1;
	  doneNode.time = -1;

	  for(int i=0;i<n.size();++i){
	    if( n[i].done || n[i].time < 0) {
	      //cout<<"Done!!"<<endl;
	    } else if(doneNode.time==-1 || n[i].time < doneNode.time) {
	      doneNode = n[i];
	      d=i;
	    }
	  }

	  if ( doneNode.time == -1 ) {
	    break;
	  }

	  //cout<<"doneNode :"<<doneNode.num<<" "<<doneNode.time<<" "<<doneNode.done<<endl;
	  n[d].done = 1;

	  for(int i=0;i<doneNode.edge_to.size();++i) {
	    int ttime= doneNode.time + doneNode.edge_time[i];
	    if(n[doneNode.edge_to[i]-1].time < 0 || ttime < n[doneNode.edge_to[i]-1].time){
	      n[doneNode.edge_to[i]-1].time = ttime;
	      //cout<<n[doneNode.edge_to[i]-1].num<<" "<<n[doneNode.edge_to[i]-1].time<<endl;
	    }
	  }

	}
	cout<<n[q].time<<endl;

      }else{

	n[p].cost = 0;
	//cout<<"n[p].time "<<n[p].cost<<endl;

	while(1){

	  Node doneNode;
	  int d=-1;
	  doneNode.cost = -1;

	  for(int i=0;i<n.size();++i){
	    if( n[i].done || n[i].cost < 0) {
	      //cout<<"Done!!"<<endl;
	    } else if(doneNode.cost==-1 || n[i].cost < doneNode.cost) {
	      doneNode = n[i];
	      d=i;
	    }
	  }

	  if ( doneNode.cost == -1 ) {
	    break;
	  }

	  //cout<<"doneNode :"<<doneNode.num<<" "<<doneNode.time<<" "<<doneNode.done<<endl;
	  n[d].done = 1;

	  for(int i=0;i<doneNode.edge_to.size();++i) {
	    int tcost= doneNode.cost + doneNode.edge_cost[i];
	    if(n[doneNode.edge_to[i]-1].cost < 0 || tcost < n[doneNode.edge_to[i]-1].cost){
	      n[doneNode.edge_to[i]-1].cost = tcost;
	      //cout<<n[doneNode.edge_to[i]-1].num<<" "<<n[doneNode.edge_to[i]-1].cost<<endl;
	    }
	  }

	}
	cout<<n[q].cost<<endl;
      }

    }
  }
}