#include <iostream>
#include <vector>

using namespace std;
static const int INF = 99999999;
int n, m;

void Dijkstra(int start, int goal, vector< vector<int> >& Vec)
{
	vector<int> cost(m);
	vector<bool> use(m);
	for(int i = 0; i < m; ++i)
	{
		cost[i] = INF;
		use[i] = false;
	}
	cost[start] = 0;
	int res = 0;
	while(true)
	{
		int v = -1;
		for(int i = 0; i < m; ++i)
		{
			if(!use[i] && (v == -1 || cost[i] < cost[v]))
			{
				v = i;
			}
		}
		if(v == -1)
		{
			break;
		}
		use[v] = true;
		for(int i = 0; i < m; ++i)
		{
			cost[i] = min(cost[i], cost[v] + Vec[v][i]);
		}
	}
	cout << cost[goal] << endl;
}

void solve()
{
	while(cin >> n >> m, n || m)
	{
		vector< vector<int> > Cost(m, vector<int>(m));
		vector< vector<int> > Time(m, vector<int>(m));
		for(int i = 0; i < m; ++i)
		{
			for(int j = 0; j < m; ++j)
			{
				Cost[i][j] = INF;
				Time[i][j] = INF;
			}
		}
		for(int i = 0; i < n; ++i)
		{
			int from, to, cost, time;
			cin >> from >> to >> cost >> time;
			--from; --to;
			Cost[from][to] = cost;
			Cost[to][from] = cost;
			Time[from][to] = time;
			Time[to][from] = time;
		}
		int k;
		cin >> k;
		for(int i = 0; i < k; ++i)
		{
			int start, goal, pattern;
			cin >> start >> goal >> pattern;
			--start; --goal;
			if(pattern == 0)
			{
				Dijkstra(start, goal, Cost);
			}
			else if(pattern == 1)
			{
				Dijkstra(start, goal, Time);
			}
		}
	}
}

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