#include<iostream>
#include<vector>
using namespace std;
class NextNodesData
{
public:
	int nextNodesName;
	int nextNodesMany;
	int nextNodesTime;
};
class Nodes
{
public:
	vector<NextNodesData> nextNodes;
	int name;
	int minValue;
};

int main()
{
	int N, M;
	cin >> N >> M;
	vector<Nodes> nodesList;
	for(int i = 0; i < M; i++)
	{
		Nodes nodes;
		nodes.name = i+1;
		nodesList.push_back(nodes);
	}
	for(int i = 0; i < N; i++)
	{
		int Start;
		int End;
		int Many;
		int Time;
		cin >> Start >> End >> Many >> Time;

		NextNodesData nodes;
		nodes.nextNodesName = End;
		nodes.nextNodesMany = Many;
		nodes.nextNodesTime = Time;
		nodesList[Start - 1].nextNodes.push_back(nodes);
	}

	int K;
	cin >> K;
	for(int i = 0; i < K; i++)
	{
		for(int j = 0; j < nodesList.size(); j++)
		{
			nodesList[j].minValue = -1;
		}
		int Start;
		int End;
		int mode;
		vector<int> candidateNodes;
		
		cin >> Start >> End;
		cin >> mode;

		nodesList[Start - 1].minValue = 0;
		candidateNodes.push_back(Start);
		int minValue = 1001;
		while(1)
		{
			minValue = 1001;
			int nextNodesName = -1;
			//????¢???????????????????????????????????¬?????¢???????????????????????????¢??????
			for(int j = 0; j < candidateNodes.size(); j++)
			{
				for(int k = 0; k < nodesList[candidateNodes[j] - 1].nextNodes.size(); k++)
				{
					bool thisNodesIsNotFixed = false;

					for(int l = 0; l < candidateNodes.size(); l++)
					{
						if(candidateNodes[l] == nodesList[candidateNodes[j] - 1].nextNodes[k].nextNodesName)
						{
							thisNodesIsNotFixed = true;
						}
					}
					if(thisNodesIsNotFixed)
					{
						continue;
					}

					int value;
					value = 0;
					if(mode == 0)
					{
						value = nodesList[candidateNodes[j]-1].minValue;
						value += nodesList[candidateNodes[j]-1].nextNodes[k].nextNodesMany;
					}
					else
					{
						value = nodesList[candidateNodes[j] - 1].minValue;
						value += nodesList[candidateNodes[j] - 1].nextNodes[k].nextNodesTime;
					}
					if(value < minValue)
					{
						minValue = value;
						nextNodesName = nodesList[candidateNodes[j] - 1].nextNodes[k].nextNodesName;
					}

				}
			}
			//???????????§??§????¬?????¢??????????????????????????±????

			nodesList[nextNodesName - 1].minValue = minValue;
			candidateNodes.push_back(nextNodesName);

			if(nextNodesName == End)
			{
				break;
			}
		}
		cout << nodesList[End - 1].minValue << endl;


	}


}