#include <iostream>
#include <vector>
#include <map>
#include <limits.h>
#include <queue>


#define INF INT_MAX

using namespace std;


struct edge {int to,cost;};
typedef pair<int,int> P;

vector<edge> ek[3002];
vector<edge> em[3002];
priority_queue<P,vector<P>,greater<P> >que;

int d[102];
int q,r;

void dik()
{
  while(!que.empty())
    {
      P p = que.top();
      que.pop();
      // cout<<p.first<<"  "<<p.second<<endl;
      int v=p.second;
      int h=p.first;
      if(v==q)
	break;
      for(int i=0;i<ek[v].size();i++)
	{
	  edge e;
	  if(r==0)
	    e=em[v][i];
	  else
	    e=ek[v][i];
	  if(d[e.to]>d[v] + e.cost)
	    {
	      d[e.to]=d[v]+e.cost;
	      que.push(P(d[e.to],e.to));
	    }
	  
	}
    }
  
}

int main()
{
  int n,m;
  for(;cin>>n>>m,n;)
    {
      //マップ初期化
      for(int i=0;i<3003;i++)
	{
	  ek[i].clear();
	  em[i].clear();
	}
      
      for(int i=0;i<n;i++)
	{
	//エッジ情報
	  int a,b,cost,time;
	  cin>>a>>b>>cost>>time;
	  edge xx;
	  xx.to=b;
	  xx.cost=cost;
	  em[a].push_back(xx);
	  xx.cost=time;
	  ek[a].push_back(xx);
	  
	}
      int c;
      cin>>c;
      for(;c--;)
	{
	  fill(d,d+102,INF);
	  //問い合わせ
	  int p;
	  cin>>p>>q>>r;
	  d[p]=0;
	  que.push(P(0,p));
	  dik();
	  cout<<d[q]<<endl;
	}
    }
}