#include<iostream>
#include<algorithm>

using namespace std;
int zikan[3001][3001];
int cost[3001][3001];


int main(){
	
	int i,j,k,a,b,t,c;
	int rail, station;
	int q,start,goal,dotti;
	int temp;
	
	


	while (1){
		
		cin >> rail >> station;
		
		if (rail == 0 && station == 0){
			break;
		}


		for (i = 0; i < station+1; i++){
			for (j = 0; j < station+1; j++){
				zikan[i][j] = 1000000;
				cost[i][j] = 1000000;
			}
		}

		for (i = 0; i < rail; i++){
			cin >> a >> b >> c >> t;
			zikan[a][b] = t;
			zikan[b][a] = t;
			cost[a][b] = c;
			cost[b][a] = c;
		}


		cin >> q;

		for (;q>0;q--){
			cin >> start >> goal >> dotti;

			for (k = 1; k <= station; k++){
				for (i = 1; i <= station; i++){
					for (j = 1; j <= station; j++){
						cost[i][j] = min(cost[i][j], cost[i][k] + cost[k][j]);
					}
				}
			}

			for (k = 1; k <= station; k++){
				for (i = 1; i <= station; i++){
					for (j = 1; j <= station; j++){
						zikan[i][j] = min(zikan[i][j], zikan[i][k] + zikan[k][j]);
					}
				}
			}


			if (dotti == 0){
				cout << cost[start][goal] << endl;
			}

			if (dotti == 1){
				cout << zikan[start][goal] << endl;
			}


		}
	


	}



}