#include <iostream>
#include <queue>
#include <utility>
#include <vector>
#include <functional>

struct Node {
	std::vector<int> edgesDest;
	std::vector<int> edgesCost[2];

	bool done = false;
	int cost = -1;

	bool operator < (const Node& n) {
		return cost < n.cost;
	}
};

int main() {
	int n, m;
	std::vector<Node> nodes;
	using Pair = std::pair<int, int>;

	while (std::cin >> n >> m) {
		if (n == 0 && m == 0)
			break;

		nodes.clear();
		nodes.resize(m);
		for (int i = 0; i < n; ++i) {
			int a, b, c, t;
			std::cin >> a >> b >> c >> t;

			nodes[a - 1].edgesDest.push_back(b - 1);
			nodes[a - 1].edgesCost[0].push_back(c);
			nodes[a - 1].edgesCost[1].push_back(t);

			nodes[b - 1].edgesDest.push_back(a - 1);
			nodes[b - 1].edgesCost[0].push_back(c);
			nodes[b - 1].edgesCost[1].push_back(t);
		}

		int k;
		std::cin >> k;
		for (int i = 0; i < k; ++i) {
			for (int j = 0; j < nodes.size(); ++j)
				nodes[j].done = false, nodes[j].cost = -1;

			int p, q, r;
			std::cin >> p >> q >> r;
			--p, --q;

			nodes[p].cost = 0;

			std::priority_queue<Pair, std::vector<Pair>, std::greater<Pair>> queue;
			queue.push(Pair(0, p));
			while (!queue.empty()) {
				const Pair& qtop = queue.top();
				Node& node = nodes[qtop.second];

				if (node.done) {
					queue.pop();
					continue;
				}
				if (qtop.second == q) {
					std::cout << node.cost << std::endl;
					break;
				}
				queue.pop();

				node.done = true;
				for (int j = 0; j < node.edgesDest.size(); ++j) {
					int dest = node.edgesDest[j];
					int cost = node.cost + node.edgesCost[r][j];

					if (nodes[dest].cost < 0 || cost < nodes[dest].cost) {
						nodes[dest].cost = cost;
						queue.push(Pair(cost, dest));
					}
				}
			}
		}
	}

	return 0;
}