#include <cstdio>
#include <cstring>
#include <algorithm>
#include <string>
#include <cmath>
#include <stack>
#include <vector>
#include <map>
#include <set>
#include <queue>
#include <list>
#include <iostream>
#include <sstream>
#include <climits>
#include <cfloat>
#include <complex>

using namespace std;

int main()
{
	int n, m;
	while (scanf("%d %d", &n, &m), n | m)
	{
		int d[2][101][101];
		for (int i = 0; i <= m; ++i)
			for (int j = 0; j <= m; ++j)
				d[0][i][j] = d[1][i][j] = 100000000;
		for (int i = 0; i <= m; ++i)
			d[0][i][i] = d[1][i][i] = 0;

		for (int i = 0; i < n; ++i)
		{
			int a, b, cost, time;
			scanf("%d %d %d %d", &a, &b, &cost, &time);
			d[0][a][b] = d[0][b][a] = cost;
			d[1][a][b] = d[1][b][a] = time;
		}
		for (int k = 0; k <= m; ++k)
			for (int i = 0; i <= m; ++i)
				for (int j = 0; j <= m; ++j)
					for (int l = 0; l < 2; ++l)
						d[l][i][j] = min(d[l][i][j], d[l][i][k] + d[l][k][j]);

		int queries;
		scanf("%d", &queries);
		for (int i = 0; i < queries; ++i)
		{
			int p, q, r;
			scanf("%d %d %d", &p, &q, &r);
			printf("%d\n", d[r][p][q]);
		}
	}

	return 0;
}