Language: C++

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

//dx[wa][wb][XCb`(0:àz, 1:Ô)] = (waÆwbÔÌàz or Ô)
int dx[MAX_N][MAX_N][2];

//p©çqÜÅÌÅ¬àzÜ½ÍÅZÔðÔ·
int tell(int p, int q, int n, int m, int r);

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ÌíÞ
			int p, q, r;
			cin >>p >>q >>r;
			
			cout <<tell(p, q, n, m, r) <<endl;
		}
	}
	return 0;
}

int tell(int p, int q, int n, int m, int r){
	
	//checkendnum[wÌÔ] = Å¬àzÜ½ÍÅZÔ(¢mèÌêÍMAX_INT)
	int checkendnum[MAX_N];
	
	//checkendi[wÌÔ] = Å¬àzÜ½ÍÅ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;
	
	//EoðÍmè³ê½m[h = q
	while(1){
		
		//SÄÌüHÌîñð²×é
		for(int i = 0; i < MAX_N; i++){
			
			//p©çiÉs­àzÜ½ÍÔÌîñª èA»êªmèµÄ¢È¯êÎ
			if(dx[node][i][r] != -1){
				if(checkendi[i] == false){
					
					//¡ÜÅiÌm[hÉüÁÄ¢½óâÌîñÆAVµ­XV³êéûÌA¬³¢ûðóâÆ·é
					checkendnum[i] = min(checkendnum[i], (dx[node][i][r] + checkendnum[node]));
				}
			
			//àµ­ÍAp©çiÉs­àzÜ½ÍÔÌîñª èA»êªmèµÄ¢È¯êÎ
			} else if(dx[i][node][r] != -1){
				if(checkendi[i] == false){
					
					//¡ÜÅiÌm[hÉüÁÄ¢½îñÆAVµ­XV³êéûÌA¬³¢ûðóâÆ·é
					checkendnum[i] = min(checkendnum[i], (dx[i][node][r] + checkendnum[node]));
				}
			}
		}
		
		//minnum = mèµÄ¢È¢wÌÅAÅ¬ÌàzÜ½ÍÅ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Ü½ÍÅZÔªmèµ½ÈçI¹(I¹ð)
		if(checkendi[q] == true){
			break;
		}
	}
	return checkendnum[q];
}

Compiler output:
a.cc: In function 'int main()':
a.cc:19:15: error: 'cin' was not declared in this scope
   19 |         while(cin >>n >>m){
      |               ^~~
a.cc:2:1: note: 'std::cin' is defined in header '<iostream>'; this is probably fixable by adding '#include <iostream>'
    1 | #include<algorithm>
  +++ |+#include <iostream>
    2 | #define MAX_M 101
a.cc:50:25: error: 'cout' was not declared in this scope
   50 |                         cout <<tell(p, q, n, m, r) <<endl;
      |                         ^~~~
a.cc:50:25: note: 'std::cout' is defined in header '<iostream>'; this is probably fixable by adding '#include <iostream>'
a.cc:50:54: error: 'endl' was not declared in this scope
   50 |                         cout <<tell(p, q, n, m, r) <<endl;
      |                                                      ^~~~
a.cc:2:1: note: 'std::endl' is defined in header '<ostream>'; this is probably fixable by adding '#include <ostream>'
    1 | #include<algorithm>
  +++ |+#include <ostream>
    2 | #define MAX_M 101