#include <iostream>
#include <vector>
#include <queue>

using namespace std;

int main(void) {
	int n,m,k,p,q,r;
	while (cin >> n >> m && n && m) {
		int dp[101][101][2];
		for (int i = 0; i < m + 1; ++i) {
			for (int j = 0; j < m + 1; ++j) {
				for (int k = 0; k < 2; ++k) {
					if (i == j) {
						dp[i][j][k] = 0;
					}
					else {
						dp[i][j][k] = 2001;
					}
				}
			}
		}

		int a,b,c,t;
		for (int i = 0; i < n; ++i) {
			cin >> a >> b >> dp[a][b][0] >> dp[a][b][1];
			dp[b][a][0] = dp[a][b][0];
			dp[b][a][1] = dp[a][b][1];
		}

		for (int mode = 0; mode < 2; ++mode) {
			for (int k = 1; k < m + 1; ++k) {
				for (int i = 1; i < m + 1; ++i) {
					for (int j = 1; j < m + 1; ++j) {
						dp[i][j][mode] = min(dp[i][j][mode], dp[i][k][mode]+dp[k][j][mode]);
					}
				}
			}
		}

		cin >> k;
		for (int i = 0; i < k; ++i) {
			cin >> p >> q >> r;
			cout << dp[p][q][r] << endl;
		}
	}
	return 0;
}