#include <stdio.h>
#include <vector>

using namespace std;

class Edge{
public:
	int from,to,cost,time;
	void swap(){
		int self = from;
		from = to;
		to = self;
	}
	int getvalue(int r){
		if(r == 0){
			return cost;
		}else{
			return time;
		}
	}
};

vector<Edge> edge;
int n,m;

int solve(const int p,const int q,const int r);

int main(){
	edge.clear();
	scanf("%d %d",&n,&m);
	for(int i=0;i<n;i++){
		Edge ed;
		scanf("%d %d %d %d",&ed.from,&ed.to,&ed.cost,&ed.time);
		edge.push_back(ed);
		ed.swap();
		edge.push_back(ed);
	}
	
	int k;
	scanf("%d",&k);
	for(int i=0;i<k;i++){
		int p,q,r;
		scanf("%d %d %d",&p,&q,&r);
		printf("%d\n",solve(p,q,r));
	}
}

int solve(const int p,const int q,const int r){
	const int INF = 2147483647;
	int a[100];
	for(int i=0;i<100;i++){
		a[i] = INF;
	}
	a[p] = 0;
	bool update;
	while(true){
		update = false;
		vector<Edge>::iterator it = edge.begin();
		while(it != edge.end()){
			if(a[(*it).from] != INF && a[(*it).to] > a[(*it).from] + (*it).getvalue(r)){
				a[(*it).to] = a[(*it).from] + (*it).getvalue(r);
				update = true;
			}
			it++;
		}
		if(!update){
			break;
		}
	}
	
	int ans = a[q];
	return a[q];
}