#include <algorithm>
#include <iostream>
#define INF 1e+9
using namespace std;

int main() {
	int n, m;
	while(cin >> n >> m, n && m) {
		int s[2][101][101];

		for(int i = 0; i < 101; ++i) {
			for(int j = 0; j < 101; ++j) {
				s[0][i][j] = (i == j) ? 0 : INF;
				s[1][i][j] = (i == j) ? 0 : INF;
			}
		}

		for(int i = 0; i < n; ++i) {
			int a, b, cost, time;
			cin >> a >> b >> cost >> time;
			s[0][a][b] = cost;
			s[0][b][a] = cost;
			s[1][a][b] = time;
			s[1][b][a] = time;
		}

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

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