#include <vector>
#include <cmath>
#include <iostream>
#include <algorithm>
#include <list>
using namespace std;

#define INF 99999999

int gcd(int x, int y) {
	if (x % y == 0)return y;
	else return gcd(y, x%y);
}

int lcm(int x, int y) {
	return x*y / gcd(x, y);
}

int c[1001][1001];
int t[1001][1001];
int main() {
	while (1) {
		int n, m;
		cin >> n >> m;
		if (n == 0)break;
		for (int i = 0; i < m + 1; i++)for (int j = 0; j < m + 1; j++) {
			c[i][j] = t[i][j] = INF;
			if (i == j)c[i][j] = t[i][j] = 0;
		}
		for (int i = 0; i < n; i++) {
			int a, b, c_, t_;
			cin >> a >> b >> c_ >> t_;
			c[a][b] = c[b][a] = c_;
			t[a][b] = t[b][a] = t_;
		}
		for (int k = 1; k < m + 1; k++)for (int i = 1; i < m + 1; i++)for (int j = 1; j < m + 1; j++) {
			c[i][j] = min(c[i][j], c[i][k] + c[k][j]);
			t[i][j] = min(t[i][j], t[i][k] + t[k][j]);
		}
		int k;
		cin >> k;
		for (int i = 0; i < k; i++) {
			int p, q, r;
			cin >> p >> q >> r;
			if (r)cout << t[p][q] << endl;
			else cout << c[p][q] << endl;
		}
	}
	return 0;
}
