#include<iostream>
#include<string>
#include<vector>
#include<cstdio>
using namespace std;
const int INF = 1 << 20;

int main()
{
	bool jud[128];
	int rail, stat, cost[128][128], time[128][128], start, goal, kind, route[128], exp[128], num;
	int wk, tk, kt, kr;
	while(cin >> rail >> stat)
	{
		if(!rail && !stat) break;
		for(int i = 0; i < stat; i++)
		{
			for(int j = 0; j < stat; j++)
			{
				cost[i][j] = INF;
				time[i][j] = INF;
			}
		}

		for(int i = 0; i < rail; i++)
		{
			cin >> wk >> tk >> kt >> kr;
			cost[wk-1][tk-1] = kt;
			cost[tk-1][wk-1] = kt;
			time[wk-1][tk-1] = kr;
			time[tk-1][wk-1] = kr;
		}

		cin >> num;

		for(int m = 0; m < num; m++)
		{
			for(int i = 0; i < stat; i++)
			{
				jud[i] = false;
				route[i] = INF;
				exp[i] = INF;
		
			}

			cin >> start >> goal >> kind;
			if(!kind)//min length
			{
				route[start - 1] = 0;
				while(1)
				{	
					int v = -1;
					for(int u = 0; u < stat; u++)
					{
						if(!jud[u] && (v == -1 || route[u] < route[v]) ) v = u;
					}

					if(v == -1) break;

					jud[v] = true;

					for(int u = 0; u < stat; u++)
					{
						route[u] = min(route[u], route[v] + cost[v][u]);
					}
				}

				cout << route[goal - 1] << endl;
			}
			else//min time
			{
				exp[start -1] = 0;
				while(1)
				{
					int v = -1;
					for(int u = 0; u < stat; u++)
					{
						if(!jud[u] && (v == -1 || exp[u] < exp[v]) ) v = u;
					}

					if(v == -1) break;

					jud[v] = true;

					for(int u = 0; u < stat; u++)
					{
						exp[u] = min(exp[u], exp[v] + time[v][u]);
					}
				}

				cout << exp[goal - 1] << endl;
			}
		}
	}
}