#include <algorithm>
#include <cstdlib>
#include <iostream>
#include <vector>


template <typename T> using Distance = std::vector<std::vector<T>>;
constexpr int INF = 100000000;


template <typename T> void in_place_init_dist(int num_vertices, Distance<T>& dist) {
	for (auto i = 0; i < num_vertices; i++) {
		for (auto j = 0; j < num_vertices; j++)
		{
			if (i == j)
			{
				dist[i][j] = 0;
			}
			else
			{
				dist[i][j] = INF;
			}
		}
	}
}


template <typename T> void in_place_warshall_floyd(int num_vertices, Distance<T>& dist) {
	for (auto k = 0; k < num_vertices; k++)
	{
		for (auto i = 0; i < num_vertices; i++)
		{
			for (auto j = 0; j < num_vertices; j++)
			{
				dist[i][j] = std::min(dist[i][j], dist[i][k] + dist[k][j]);
			}
		}
	}
}


int main() {
	std::cin.tie(nullptr);
	std::ios::sync_with_stdio(false);
	while (true)
	{
		int n, m;
		std::cin >> n >> m;
		if (n == 0 && m == 0)
		{
			break;
		}
		Distance<int> cost;
		Distance<int> time;
		cost = Distance<int>(m, std::vector<int>(m, 0));
		time = Distance<int>(m, std::vector<int>(m, 0));
		in_place_init_dist(m, cost);
		in_place_init_dist(m, time);
		int a, b, c, t;
		for (auto i = 0; i < n; i++)
		{
			std::cin >> a >> b >> c >> t;
			a--;
			b--;
			cost[a][b] = c;
			cost[b][a] = c;
			time[a][b] = t;
			time[b][a] = t;
		}
		in_place_warshall_floyd(m, cost);
		in_place_warshall_floyd(m, time);
		int k, p, q, r;
		std::cin >> k;
		for (auto i = 0; i < k; i++)
		{
			std::cin >> p >> q >> r;
			p--;
			q--;
			std::cout << (r == 0 ? cost[p][q] : time[p][q]) << std::endl;
		}
	}

	return EXIT_SUCCESS;
}