#include<iostream>
#include<vector>
#include<queue>
#define MAX_M 101
using namespace std;

struct list{
	int a;
	int b;
	int cost;
	int time;
	struct list *next;
};

//Å¬àzðÔ·
int checkcost(int m, int p, int q, struct list *head);

//ÅZÔðÔ·
int checktime(int m, int p, int q, struct list *head);

//XgÉVKo^
struct list *addlist(int a, int b, int cost, int time, struct list *head);

int main(){
	//n = üHÌîñÌ, m = wÌ
	int n, m;
	while(cin >>n >>m){
		if(n == 0 && m == 0) break;
		struct list *head;
		head = NULL;
		for(int i = 0; i < n; i++){
			int a, b, cost, time;
			cin >>a >>b >>cost >>time;
			head = addlist(a, b, cost, time, head);
		}
		//k = â¢í¹Ì, p = o­n, q = ÚIn, r = oÍ·élÌíÞ(0 : Å¬àz, 1 : ÅZÔ)
		int k, p, q, r;
		cin >>k;
		for(int i = 0; i < k; i++){
			cin >>p >>q >>r;
			int ans;
			if(r == 0){
				ans = checkcost(m, p, q, head);
			} else{
				ans = checktime(m, p, q, head);
			}
			cout <<ans <<endl;
		}
	}
	return 0;
}

int checkcost(int m, int p, int q, struct list *head){
	//Y¦ÌÔðÂwÉs­½ßÌAÅ¬àzªmèµÄ¢é©Ç¤©ð²×ézñ
	//(mèµÄ¢éê:»Ìàz, mèµÄ¢È¢ê:-1)
	int checkend[MAX_M + 1] = {-1};
	for(int i = 0; i < MAX_M; i++){
		checkend[i] = -1;
	}
	
	checkend[p] = 0;
	
	//Y¦ÌÔðÂwÉs­½ßÌAÅ¬àzÌóâðüêévector
	vector<int> costlist[MAX_M + 1];
	
	while(1){
		
		//`FbN·×«costlistÌY¦ðÛ¶·équeue
		queue<int> check_i;
		
		struct list *po = head;
		
		while(po != NULL){
			
			//cout <<"po->a = " <<po->a <<" po->b = " <<po->b <<endl;
			
			//po->aÌwÌàzªmèµÄ¨èApo->bÌwÌàzªmèµÄ¢È¢Æ«
			if((checkend[po->a] != -1 && checkend[po->b] == -1)){
				
				int temp = po->cost + checkend[po->a];
				costlist[po->b].push_back(temp);
				check_i.push(po->b);
				//cout <<"case1 : temp = " <<po->cost <<" + " <<checkend[po->a] <<" push = " <<po->b <<endl;
				//cout <<"in = " <<costlist[po->b][0] <<endl;
				
			//po->bÌwÌàzªmèµÄ¨èApo->aÌwÌàzªmèµÄ¢È¢Æ«
			} else if((checkend[po->a] == -1 && checkend[po->b] != -1)){
				int temp = po->cost + checkend[po->b];
				costlist[po->a].push_back(temp);
				check_i.push(po->a);
				//cout <<"case2 : temp = " <<po->cost <<" + " <<checkend[po->b] <<" push = " <<po->a <<endl;
			}
			po = po->next;
		}
		
		//cout <<"end" <<endl;
		
		int tempi = check_i.front();
		int mincost = costlist[tempi][0], mini = tempi;
		while(1){
			check_i.pop();
			
			//check_iÅwèµ½Y¦Ìcostlist[tempi]Ì©çAÅ¬ÌàzÆ»ÌY¦ðIÔ
			for(int k = 0; k < costlist[tempi].size(); k++){
				//cout <<"costlist[" <<tempi <<"][" <<k <<"] = " <<costlist[tempi][k] <<endl;
				if(mincost > costlist[tempi][k]){
					mincost = costlist[tempi][k];
					mini = tempi;
				}
			}
			if(check_i.empty()) break;
			tempi = check_i.front();
		}
		//²×½Å¬ÌàzÆ»ÌY¦ðcheckend[i]Éüêé
		//cout <<"@mini = " <<mini <<" mincost = " <<mincost <<endl;
		checkend[mini] = mincost;
		
		for(int i = 0; i < m; i++){
			//cout <<"checkend[" <<i <<"] = " <<checkend[i] <<endl;
		}
		
		//ß½¢wÌÅ¬àzðmèµI¦½©Ì`FbN
		if(checkend[q] != -1) break;
	}
	return checkend[q];
}

int checktime(int m, int p, int q, struct list *head){
	//Y¦ÌÔðÂwÉs­½ßÌAÅ¬àzªmèµÄ¢é©Ç¤©ð²×ézñ
	//(mèµÄ¢éê:»Ìàz, mèµÄ¢È¢ê:-1)
	int checkend[MAX_M + 1] = {-1};
	for(int i = 0; i < MAX_M; i++){
		checkend[i] = -1;
	}
	
	checkend[p] = 0;
	
	//Y¦ÌÔðÂwÉs­½ßÌAÅ¬àzÌóâðüêévector
	vector<int> timelist[MAX_M + 1];
	
	while(1){
		
		//`FbN·×«costlistÌY¦ðÛ¶·équeue
		queue<int> check_i;
		
		struct list *po = head;
		
		while(po != NULL){
			
			//cout <<"po->a = " <<po->a <<" po->b = " <<po->b <<endl;
			
			//po->aÌwÌàzªmèµÄ¨èApo->bÌwÌàzªmèµÄ¢È¢Æ«
			if((checkend[po->a] != -1 && checkend[po->b] == -1)){
				
				int temp = po->time + checkend[po->a];
				timelist[po->b].push_back(temp);
				check_i.push(po->b);
				//cout <<"case1 : temp = " <<po->cost <<" + " <<checkend[po->a] <<" push = " <<po->b <<endl;
				//cout <<"in = " <<costlist[po->b][0] <<endl;
				
			//po->bÌwÌàzªmèµÄ¨èApo->aÌwÌàzªmèµÄ¢È¢Æ«
			} else if((checkend[po->a] == -1 && checkend[po->b] != -1)){
				int temp = po->time + checkend[po->b];
				timelist[po->a].push_back(temp);
				check_i.push(po->a);
				//cout <<"case2 : temp = " <<po->cost <<" + " <<checkend[po->b] <<" push = " <<po->a <<endl;
			}
			po = po->next;
		}
		
		//cout <<"end" <<endl;
		
		int tempi = check_i.front();
		int mintime = timelist[tempi][0], mini = tempi;
		while(1){
			check_i.pop();
			
			//check_iÅwèµ½Y¦Ìcostlist[tempi]Ì©çAÅ¬ÌàzÆ»ÌY¦ðIÔ
			for(int k = 0; k < timelist[tempi].size(); k++){
				//cout <<"costlist[" <<tempi <<"][" <<k <<"] = " <<costlist[tempi][k] <<endl;
				if(mintime > timelist[tempi][k]){
					mintime = timelist[tempi][k];
					mini = tempi;
				}
			}
			if(check_i.empty()) break;
			tempi = check_i.front();
		}
		//²×½Å¬ÌàzÆ»ÌY¦ðcheckend[i]Éüêé
		//cout <<"@mini = " <<mini <<" mincost = " <<mincost <<endl;
		checkend[mini] = mintime;
		
		for(int i = 0; i < m; i++){
			//cout <<"checkend[" <<i <<"] = " <<checkend[i] <<endl;
		}
		
		//ß½¢wÌÅ¬àzðmèµI¦½©Ì`FbN
		if(checkend[q] != -1) break;
	}
	return checkend[q];
}

struct list *addlist(int a, int b, int cost, int time, struct list *head){
	struct list *po;
	
	if ((po = (struct list *) malloc(sizeof(struct list))) == NULL) {
		printf("malloc error\n");
		exit(EXIT_FAILURE);
	}
	
	po->a = a;
	po->b = b;
	po->cost = cost;
	po->time = time;
	po->next = head;
	head = po;
	
	return head;
}