#include <iostream>
#include <vector>
#include <queue>
#include <functional>
using namespace std;

int n, m;
int k;
int cost[100][100];
int time[100][100];

int main()
{
	memset(cost, 0x0f, sizeof cost);
	memset(time, 0x0f, sizeof time);

	cin >> n >> m;
	for (int i = 0; i < n; ++i)
	{
		int a, b, cost_, time_;
		cin >> a >> b >> cost_ >> time_;

		cost[a - 1][b - 1] = cost_;
		cost[b - 1][a - 1] = cost_;

		time[a - 1][b - 1] = time_;
		time[b - 1][a - 1] = time_;
	}

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

	cin >> k;
	for (int i = 0; i < k; ++i)
	{
		int p, q, r;
		cin >> q >> p >> r;
		if (r == 0)
		{
			cout << cost[q - 1][p - 1] << endl;
		}
		else
		{
			cout << time[q - 1][p - 1] << endl;
		}
	}
}