# include <iostream>
# include <algorithm>

# define INF 1e6
# define MAX_V 100

int e, v, K;

int d[2][MAX_V][MAX_V];

int main(){
	while (1){
		for (int i = 0; i < v; ++i){
			for (int j = 0; j < v; ++j){
				d[0][i][j] = (i == j) ? 0 : INF;
				d[1][i][j] = (i == j) ? 0 : INF;
			}
		}
		int a, b, cost, time;
		std::cin >> e >> v;
		if ((e + v) == 0)
			break;
		for (int i = 0; i < e; ++i){
			std::cin >> a >> b >> cost >> time;
			d[0][a][b] = cost;
			d[0][b][a] = cost;
			d[1][a][b] = time;
			d[1][b][a] = time;
		}
		for (int k = 0; k < v; ++k){
			for (int i = 0; i < v; ++i){
				for (int j = 0; j < v; ++j){
					d[0][i][j] = std::min(d[0][i][j], d[0][i][k] + d[0][k][j]);
					d[1][i][j] = std::min(d[1][i][j], d[1][i][k] + d[1][k][j]);
				}
			}
		}
		int p, q, r;
		std::cin >> K;
		for (int i = 0; i < K; ++i){
			std::cin >> p >> q >> r;
			std::cout << d[r][p - 1][q - 1] << std::endl;
		}
	}
	return 0;
}