#include<iostream>
#include<algorithm>
#define MAX_M 101
#define MAX_N 301
#define MAX_K 201
#define MAX_INT 1 << 29;
using namespace std;

//dx[wa][wb][XCb`(0:àz, 1:Ô)] = (waÆwbÔÌàz or Ô)
int dx[MAX_N][MAX_N][2];

//p©çqÜÅÌÅ¬àzðÔ·
int tellcost(int p, int q, int n, int m);

//p©çqÜÅÌÅZÔðÔ·
int telltime(int p, int q, int n, int m);

int main(){
	//n = üHÌîñÌ, m = wÌ
	int n, m;
	
	//üHÌîñðüÍ
	while(cin >>n >>m){
		if(n == 0 && m == 0) break;
		
		//dx[][][]Ìú»(SÄ-1É·é)
		for(int i = 0; i < MAX_N; i++){
			for(int j = 0; j < MAX_N; j++){
				dx[i][j][0] = -1;
				dx[i][j][1] = -1;
			}
		}
		
		for(int i = 0; i < n; i++){
			
			//a = wa, b = wb, cost = wa©çwbÜÅÌàz, time = wa©çwbÜÅÌÔ
			int a, b, cost, time;
			
			cin >>a >>b >>cost >>time;
			dx[a][b][0] = cost;
			dx[a][b][1] = time;
		}
		
		//k = â¢í¹Ì
		int k;
		
		cin >>k;
		for(int i = 0; i < k; i++){
			
			//p = o­w, q = w, r = oÍ·élÌíÞ, ans = Ö©çAÁÄ«½lðüêéÏ(±êª¦)
			int p, q, r, ans;
			cin >>p >>q >>r;
			
			//oÍ·élÌíÞª0(Å¬àz)Èç
			if(r == 0){
				ans = tellcost(p, q, n, m);
			
			//oÍ·élÌíÞª1(ÅZÔ)Èç
			} else{
				ans = telltime(p, q, n, m);
			}
			cout <<ans <<endl;
		}
	}
	return 0;
}

int tellcost(int p, int q, int n, int m){
	
	//checkendnum[wÌÔ] = Å¬àz(¢mèÌêÍMAX_INT)
	int checkendnum[MAX_N];
	
	//checkendi[wÌÔ] = Å¬àzªmèµ½©Ç¤©(false:¢mè, true:mè)
	bool checkendi[MAX_N] = {false};
	
	//checkendnum[1 ` MAX_N]ðMAX_INTÅú»
	for(int i = 0; i < MAX_N; i++){
		checkendnum[i] = MAX_INT;
	}
	
	//p = ¡²×Ä¢ém[h
	int node = p;
	
	//o­wÍà¤0ÅmèµÄ¢é
	checkendnum[node] = 0;
	checkendi[node] = true;
	
	//EoðÍmè³ê½m[h = q
	while(1){
		
		//SÄÌüHÌîñð²×é
		for(int i = 0; i < n; i++){
			
			//p©çiÉs­àzÌîñª èA»êªmèµÄ¢È¯êÎ
			if(dx[node][i][0] != -1){
				if(checkendi[i] == false){
					
					//¡ÜÅiÌm[hÉüÁÄ¢½îñÆAVµ­XV³êéûÌA¬³¢ûðóâÆ·é
					checkendnum[i] = min(checkendnum[i], (dx[node][i][0] + checkendnum[node]));
				}
			
			//p©çiÉs­àzÌîñª èA»êªmèµÄ¢È¯êÎ
			} else if(dx[i][node][0] != -1){
				if(checkendi[i] == false){
					
					//¡ÜÅiÌm[hÉüÁÄ¢½îñÆAVµ­XV³êéûÌA¬³¢ûðóâÆ·é
					checkendnum[i] = min(checkendnum[i], (dx[i][node][0] + checkendnum[node]));
				}
			}
		}
		//minnum = mèµÄ¢È¢wÌÅAÅ¬Ìàz, mini = minnumðÂwÌÔ
		int minnum = MAX_INT;
		int mini = -1;
		
		//SÄÌwð²×é
		for(int i = 1; i <= m; i++){
			
			//àzªmèµÄ¨ç¸Aminnumæè¬³¢àzðÂÈç
			if(checkendi[i] == false && checkendnum[i] < minnum){
				minnum = checkendnum[i];
				mini = i;
			}
		}
		
		//miniÌwÌàzðmè
		checkendi[mini] = true;
		
		//Ì²×ém[hðminiÉ·é
		node = mini;
		
		//qÌÅ¬àzªmèµ½ÈçI¹(I¹ð)
		if(checkendi[q] == true){
			break;
		}
	}
	return checkendnum[q];
}

int telltime(int p, int q, int n, int m){
	
	//checkendnum[wÌÔ] = ÅZÔ(¢mèÌêÍMAX_INT)
	int checkendnum[MAX_N];
	
	//checkendi[wÌÔ] = ÅZÔªmèµ½©Ç¤©(false:¢mè, true:mè)
	bool checkendi[MAX_N] = {false};
	
	//checkendnum[1 ` MAX_N]ðMAX_INTÅú»
	for(int i = 0; i < MAX_N; i++){
		checkendnum[i] = MAX_INT;
	}
	
	//p = ¡²×Ä¢ém[h
	int node = p;
	
	//o­wÍà¤0ÅmèµÄ¢é
	checkendnum[node] = 0;
	checkendi[node] = true;
	
	//EoðÍmè³ê½m[h = q
	while(1){
		
		//SÄÌüHÌîñð²×é
		for(int i = 0; i < n; i++){
			
			//p©çiÉs­ÔÌîñª èA»êªmèµÄ¢È¯êÎ
			if(dx[node][i][1] != -1){
				if(checkendi[i] == false){
					
					//¡ÜÅiÌm[hÉüÁÄ¢½îñÆAVµ­XV³êéûÌA¬³¢ûðóâÆ·é
					checkendnum[i] = min(checkendnum[i], (dx[node][i][1] + checkendnum[node]));
				}
			
			//p©çiÉs­àzÌîñª èA»êªmèµÄ¢È¯êÎ
			} else if(dx[i][node][1] != -1){
				if(checkendi[i] == false){
					
					//¡ÜÅiÌm[hÉüÁÄ¢½îñÆAVµ­XV³êéûÌA¬³¢ûðóâÆ·é
					checkendnum[i] = min(checkendnum[i], (dx[i][node][1] + checkendnum[node]));
				}
			}
		}
		//minnum = mèµÄ¢È¢wÌÅAÅZÌÔ, mini = minnumðÂwÌÔ
		int minnum = MAX_INT;
		int mini = -1;
		
		//SÄÌwð²×é
		for(int i = 1; i <= m; i++){
			
			//ÔªmèµÄ¨ç¸AminnumæèZ¢ÔðÂÈç
			if(checkendi[i] == false && checkendnum[i] < minnum){
				minnum = checkendnum[i];
				mini = i;
			}
		}
		
		//miniÌwÌÔðmè
		checkendi[mini] = true;
		
		//Ì²×ém[hðminiÉ·é
		node = mini;
		
		//qÌÅZÔªmèµ½ÈçI¹(I¹ð)
		if(checkendi[q] == true){
			break;
		}
	}
	return checkendnum[q];
}