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

#define MMAX 101
const int INF=1<<10;

int gtime[MMAX+1][MMAX+1];
int gcost[MMAX+1][MMAX+1];

int n,m;
int p,q,r;

struct node{
	int st;//
	int cost;//
}typedef Node;

bool operator < (const Node n1,const Node n2){
	return n1.cost > n2.cost;
}
bool visited[MMAX];
int dijkstra(){
	fill(visited,visited+MMAX,false);
	priority_queue<Node> pq;
	Node start={p,0};
	pq.push(start);
	while(!pq.empty()){
		Node cur=pq.top();pq.pop();
		if(cur.st==q)return cur.cost;
		if(visited[cur.st])continue;
		visited[cur.st]=true;
		for(int i=1;i<=m;i++){
			if(cur.st==i||gtime[cur.st][i]==INF||gcost[cur.st][i]==INF)continue;
			if(visited[i])continue;
			if(r==1){
				Node add={i,cur.cost+gtime[cur.st][i]};
				pq.push(add);
			}
			else {//
				Node add={i,cur.cost+gcost[cur.st][i]};
				pq.push(add);
			}
		}
	}
}
void solve(){
	cout<<dijkstra()<<endl;
}
int main(){
	int a,b,cost,time,k;
	while(cin>>n>>m,(n||m)){
		for(int i=0;i<MMAX;i++){
			fill(gtime[i],gtime[i]+MMAX,INF);
			fill(gcost[i],gcost[i]+MMAX,INF);
		}
		for(int i=0;i<n;i++){
			cin>>a>>b>>cost>>time;
			if(gtime[a][b]>time)gtime[a][b]=gtime[b][a]=time;
			if(gcost[a][b]>cost)gcost[a][b]=gcost[b][a]=cost;
		}
		cin>>k;
		for(int i=0;i<k;i++){
			cin>>p>>q>>r;
			solve();
		}		
	}
	return 0;
}