#include<iostream>
#include<algorithm>
using namespace std;

bool open(int max[101],int name[101][101][2],int m,int basic,int r)
{
	int i;
	bool F5=false;
	for(i=1;i<=m;i++)
	{
		if(basic == i)
			continue;
		if(name[basic][i][r] != 0)
		{
			if(max[i] == -1)
			{
				max[i] = name[basic][i][r] + max[basic];
				if(F5 == false)
					F5 = true;
			}
			else if(max[i] > name[basic][i][r] + max[basic])
			{
				max[i] = name[basic][i][r] + max[basic];
				if(F5 == false)
					F5 = true;
			}
		}
	}
	return F5;
}

int main(void)
{
	while(1)
	{
		int n,m;
		int a,b,cost,time;
		int k;
		int p,q,r;
		int mintime;
		int mincost;
		int i,j,x,y;
		bool F5;

		int name[101][101][2] = {};//OÍ¿à
		int max[101]={};

		cin >> n >> m;

		if(n==0 && m==0)
			return 0;

		for(i=0;i<n;i++)
		{
			cin >> a >> b >> cost >> time;
			name[a][b][0] = cost;
			name[b][a][0] = cost;
			name[a][b][1] = time;
			name[b][a][1] = time;
		}

		cin >> k;

		for(i=0;i<k;i++)
		{
			F5 = true;
			cin >> p >> q >> r;

			for(j=0;j<101;j++)
			{
				max[j] = -1;
			}

			max[p] = 0;

			while(F5 == true)
			{
				F5 = false;
				for(x=1;x<=n;x++)
				{
					if(open(max,name,m,x,r) == true)
					{
						if(F5 == false)
						{
							F5 = true;
						}
					}
				}
			}
			cout << max[q] << endl;
		}
	}
	return 0;
}