#include <cstdio>
#include <vector>

using namespace std;

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

vector<Edge> edge;
int n,m;

const int INF = 100000000;

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

int main(){
	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;
	int p,q,r;
	scanf("%d",&k);
	for(int i=0;scanf("%d %d",&p,&q) && p && q && i < k;i++){
		scanf("%d",&r);
		printf("%d\n",solve(p,q,r));
	}
	return 0;
}

int solve(const int p,const int q,const int r){
	vector<int> d(m+1);
	for(int i=0;i<=m;i++){
		d[i] = INF;
	}
	d[p] = 0;
	
	bool flag;
	while(1){
		flag = false;
		vector<Edge>::iterator it;
		for(it = edge.begin();it != edge.end();it++){
			if(d[(*it).from] != INF && d[(*it).to] > d[(*it).from] + (*it).GetValue(r)){
				d[(*it).to] = d[(*it).from] + (*it).GetValue(r);
				flag = true;
			}
		}
		if(!flag){
			break;
		}
	}
	
	return d[q];
}