#include<iostream>
#include<algorithm>
#include<vector>
#include<queue>

using namespace std;

const int INF=1e9;

//struct edge{int to,cost,time;};
struct edge{int cost,time;};

int main(){
	int n,m;
	cin >> n >> m;

	//vector<edge> es[n];
	edge es[m][m];
	for(int i=0;i<m;i++)for(int j=0;j<m;j++) es[i][j] = (edge){INF,INF};

	int a,b,cost,time;
	for(int i=0;i<n;i++){
		cin >> a >> b >> cost >> time;
	//	es[a].emplace_back((edge){b,cost,time});
		es[a-1][b-1] = (edge){cost,time};
		es[b-1][a-1] = es[a-1][b-1];
	}

	int d_cost[m][m];
	int d_time[m][m];
	for(int i=0;i<m;i++){
		for(int j=0;j<m;j++){
			d_cost[i][j] = es[i][j].cost;
			d_time[i][j] = es[i][j].time;
		}
	}

	for(int k=0;k<m;k++){
		for(int i=0;i<m;i++){
			for(int j=0;j<m;j++){
				d_cost[i][j] = min(d_cost[i][j],d_cost[i][k] + d_cost[k][j]);
				d_time[i][j] = min(d_time[i][j],d_time[i][k] + d_time[k][j]);
			}
		}
	}

	int k;
	cin >> k;
	for(int n=0;n<k;n++){
		int p,q,r;
		cin >> p >> q >> r;

		if(r==0) cout << d_cost[p-1][q-1] << "\n";
		else	cout << d_time[p-1][q-1] << "\n";
	}
	cin >> k >> k;

	return 0;
}

