/*****include*****/
#include <iostream>
#include <fstream>

/*****デバッグ定義*****/
//#define DEBUG
#define INF 10000
/*****マクロ定義*****/

/*****名前空間*****/
using namespace std;

/*****グローバル変数置き場*****/
typedef struct{
	int time;
	int cost;
}Field;
Field field[100][100];
/*****その他関数置き場*****/
int djikstra(int start,int goal,int num,int order){
	int *dis;
	bool *used;
	dis = new int [num];
	used = new bool [num];
	int station;
	int res;
	for(int i=0;i<num;i++){
		dis[i] = INF;
		used[i] = false;
	}
	dis[start] = 0;
	while(true){
		station = -1;
		for(int i=0;i<num;i++){
			if(!used[i] && (station == -1 || dis[i] < dis[station])){
				station = i;
			}
		}
		if(station == -1)	break;
		used[station] = true;
		for(int i=0;i<num;i++){
			int adder;
			adder = ((order) ? field[station][i].time : field[station][i].cost);
			if(dis[i] > dis[station]+adder){
				dis[i] = dis[station]+adder;
			}
		}
	}
	res = dis[goal];
	delete dis;
	delete used;
	return res;
}
/*****main関数*****/
int main(){
	/*****ファイルオープン*****/
#ifdef DEBUG
	ofstream fout("output.txt");
	ifstream fin("input.txt");
	if(!fout || !fin){
		cout << "Can't open the file.\n";
		return;
	}
#endif
	/*****変数置き場*****/
	int rail_num,sta_num;
	while(1){
		for(int i=0;i<100;i++){
			for(int j=0;j<100;j++){
				field[i][j].time = INF;
				field[i][j].cost = INF;
			}
		}
		cin >> rail_num >> sta_num;
		if(rail_num == 0 && sta_num == 0)	break;
		for(int i=0;i<rail_num;i++){
			int st1,st2,cost,time;
			cin >> st1 >> st2 >> cost >> time;
			field[st1-1][st2-1].cost = cost;
			field[st2-1][st1-1].cost = cost;
			field[st1-1][st2-1].time = time;
			field[st2-1][st1-1].time = time;
			field[st1-1][st1-1].cost = 0;
			field[st2-1][st2-1].time = 0;
		}
		int loop;
		int min;
		cin >> loop;
		for(int i=0;i<loop;i++){
			int order,start,goal;
			cin >> start >> goal >> order;
			if(order==1){
				min = djikstra(start-1,goal-1,sta_num,order);
			}else{
				min = djikstra(start-1,goal-1,sta_num,order);
			}
			cout << min << endl;
		}
	}
	/*****処理部*****/
	
	/*****処理終了後*****/
#ifdef DEBUG
	fout.close();
	fin.close();
#endif
	return 0;
}