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

void open(int max[101],int name[101][101][2],int m,int basic,int r,int to,vector<int> got[101])
{
	unsigned int i;
	for(i=0;i<got[basic].size();i++)
	{
		if(max[got[basic][i]] == -1 || max[got[basic][i]] > max[basic] + name[basic][got[basic][i]][r])
		{
			max[got[basic][i]] = max[basic] + name[basic][got[basic][i]][r];
			if(got[basic][i] != to)
				open(max,name,m,got[basic][i],r,to,got);
		}
	}
}

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;

		int name[101][101][2] = {};//OÍ¿à
		int max[101]={};
		vector<int> got[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;
			got[a].push_back(b);
			got[b].push_back(a);
		}

		cin >> k;

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

			fill(max , max+101 , -1);

			max[p] = 0;

			open(max,name,m,p,r,q,got);

			cout << max[q] << endl;
		}
	}
	return 0;
}