#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <climits>
#include <cfloat>
#include <ctime>
#include <map>
#include <utility>
#include <set>
#include <iostream>
#include <memory>
#include <string>
#include <vector>
#include <algorithm>
#include <functional>
#include <sstream>
#include <complex>
#include <stack>
#include <queue>
#include <numeric>
#include <list>

using namespace std;

#ifdef _MSC_VER
#define __typeof__ decltype
template <class T> int __builtin_popcount(T n) { return n ? 1 + __builtin_popcount(n & (n - 1)) : 0; }
#endif

#define foreach(it, c) for (__typeof__((c).begin()) it=(c).begin(); it != (c).end(); ++it)
#define all(c) (c).begin(), (c).end()
#define rall(c) (c).rbegin(), (c).rend()
#define popcount __builtin_popcount
#define CLEAR(arr, val) memset(arr, val, sizeof(arr))

#define rep(i, n) for (int i = 0; i < n; ++i)

template <class T> void max_swap(T& a, const T& b) { a = max(a, b); }
template <class T> void min_swap(T& a, const T& b) { a = min(a, b); }

typedef long long ll;
typedef pair<int, int> pint;

const double PI = acos(-1.0);
const int dx[] = { 0, 1, 0, -1 };
const int dy[] = { 1, 0, -1, 0 };

struct edge
{
	int to, cost;
	edge(int to, int cost)
		: to(to), cost(cost) {}
};
typedef vector<vector<edge> > graph;
void add_edge(graph& g, int a, int b, int cost)
{
	g[a].push_back(edge(b, cost));
	g[b].push_back(edge(a, cost));
}
void add_dir_edge(graph& g, int a, int b, int cost)
{
	g[a].push_back(edge(b, cost));
}
vector<int> dijkstra(const graph& g, int s)
{
	const int _INF = 1e9;
	typedef pair<int, int> pint;

	vector<int> dis(g.size(), _INF);
	priority_queue<pint, vector<pint>, greater<pint> > q;

	dis[s] = 0;
	q.push(pint(0, s));
	while (!q.empty())
	{
		pint t = q.top(); q.pop();
		int cur = t.second, d = t.first;

		if (d > dis[cur])
			continue;

		for (int i = 0; i < g[cur].size(); ++i)
		{
			const edge& e = g[cur][i];
			int next_d = d + e.cost;
			if (next_d < dis[e.to])
			{
				dis[e.to] = next_d;
				q.push(pint(next_d, e.to));
			}
		}
	}

	return dis;
}

int main()
{
	int n, m;
	while (cin >> n >> m, n | m)
	{
		graph cost(m), time(m);
		for (int i = 0; i < n; ++i)
		{
			int a, b, c, t;
			cin >> a >> b >> c >> t;
			--a, --b;

			add_edge(cost, a, b, c);
			add_edge(time, a, b, t);
		}

		vector<int> min_cost[128], min_time[128];
		for (int i = 0; i < m; ++i)
		{
			min_cost[i] = dijkstra(cost, i);
			min_time[i] = dijkstra(time, i);
		}

		int k;
		cin >> k;
		while (k--)
		{
			int p, q, r;
			cin >> p >> q >> r;
			--p, --q;
			int res = r == 0 ? min_cost[p][q] : min_time[p][q];
			cout << res << endl;
		}
	}
}