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

using namespace std;
typedef pair<int, int> P;
static const int INF = 99999999;
struct SEdge
{
	int To;
	int Cost;
	int Time;
};
int V, E;
vector<int> d;
vector< vector<SEdge> > G;

void Dijkstra_Cost(int s)
{
	fill(d.begin(), d.end(), INF);
	d[s] = 0;
	priority_queue<P, vector<P>, greater<P> > PQue;
	PQue.push(P(0, s));
	while(!PQue.empty())
	{
		P p = PQue.top();
		PQue.pop();
		int v = p.second;
		if(d[v] < p.first)
		{
			continue;
		}
		for(int i = 0; i < G[v].size(); ++i)
		{
			SEdge e = G[v][i];
			if(d[e.To] > d[v] + e.Cost)
			{
				d[e.To] = d[v] + e.Cost;
				PQue.push(P(d[e.To], e.To));
			}
		}
	}
}

void Dijkstra_Time(int s)
{
	fill(d.begin(), d.end(), INF);
	d[s] = 0;
	priority_queue<P, vector<P>, greater<P> > PQue;
	PQue.push(P(0, s));
	while(!PQue.empty())
	{
		P p = PQue.top();
		PQue.pop();
		int v = p.second;
		if(d[v] < p.first)
		{
			continue;
		}
		for(int i = 0; i < G[v].size(); ++i)
		{
			SEdge e = G[v][i];
			if(d[e.To] > d[v] + e.Time)
			{
				d[e.To] = d[v] + e.Time;
				PQue.push(P(d[e.To], e.To));
			}
		}
	}
}

void solve()
{
	while(cin >> E >> V, E || V)
	{
		d.clear();
		d.resize(V);
		for(int i = 0; i < G.size(); ++i)
		{
			G[i].clear();
		}
		G.clear();
		G.resize(V);
		for(int i = 0; i < E; ++i)
		{
			int From, To, Cost, Time;
			cin >> From >> To >> Cost >> Time;
			--From;
			--To;
			SEdge e1, e2;
			e1.To = To;
			e2.To = From;
			e1.Cost = e2.Cost = Cost;
			e1.Time = e2.Time = Time;
			G[From].push_back(e1);
			G[To].push_back(e2);
		}
		int n;
		cin >> n;
		for(int i = 0; i < n; ++i)
		{
			int p, q, r;
			cin >> p >> q >> r;
			--p;
			--q;
			if(r == 0)
			{
				Dijkstra_Cost(p);
			}
			else
			{
				Dijkstra_Time(p);
			}
			cout << d[q] << endl;
		}
	}
}

int main()
{
	solve();
	return(0);
}