#include <cstdio>
#include <vector>
#include <queue>
#include <functional>

using namespace std;

#define fst first
#define scd second
#define PB push_back
#define MP make_pair
#define rep(i,x) for(int i=0;i<(x);++i)

typedef pair<int, int> pii;

const int inf = 1e9;

struct edge {
	int to, cost, time;
	edge(int to0,int cost0,int time0) {
		to = to0; cost = cost0; time = time0;
	};
};

vector<edge> G[100];
int d[100];

int N, M;

void dijkstra(int s, int mode)
{
	priority_queue<pii, vector<pii>, greater<pii> > q;

	fill(d, d + M, inf);
	d[s] = 0;

	q.push(MP(0, s));

	while (q.size()) {
		pii p = q.top(); q.pop();
		int v = p.scd;
		if (d[v] < p.fst) continue;
		rep(u, G[v].size()) {
			edge e = G[v][u];
			int a = mode == 0 ? e.cost : e.time;
			if (d[e.to] > d[v] + a) {
				d[e.to] = d[v] + a;
				q.push(MP(d[e.to], e.to));
			}
		}
	}
}

signed main()
{
	while (scanf("%d %d", &N, &M), N || M) {
		rep(i, M) G[i].clear();

		rep(i, N) {
			int a, b, cost, time; scanf("%d %d %d %d", &a, &b, &cost, &time); a--; b--;
			G[a].PB(edge(b, cost, time));
			G[b].PB(edge(a, cost, time));
		}

		int K; scanf("%d", &K);

		rep(i, K) {
			int p, q, r; scanf("%d %d %d", &p, &q, &r); p--; q--;
			dijkstra(p, r);
			printf("%d\n", d[q]);
		}
	}
}