#include <iostream>
#include <algorithm>
#include <string.h>
using namespace std;

int n, m;
int k;
int cost_table[100][100];
int time_table[100][100];

int main()
{
	memset(cost_table, 0x0f, sizeof cost_table);
	memset(time_table, 0x0f, sizeof time_table);

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

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

		time_table[a - 1][b - 1] = time;
		time_table[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_table[i][j] = min(cost_table[i][j], cost_table[i][k] + cost_table[k][j]);
			}
		}
	}
	for (int k = 0; k < m; ++k)
	{
		for (int i = 0; i < m; ++i)
		{
			for (int j = 0; j < m; ++j)
			{
				time_table[i][j] = min(time_table[i][j], time_table[i][k] + time_table[k][j]);
			}
		}
	}

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