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

struct AboutTo;

class Node{
public:
	int aa;
	int Lowest;
	bool IsSelected;
	vector<AboutTo> ToCollection;
	Node(){
		Lowest = 2147483647/2;
		IsSelected = false;
	}
	void Reset(){
		Lowest = 2147483647/2;
		IsSelected = false;
	}
};
struct AboutTo{
	Node* To;
	int Cost;
	int Time;
};

int GetShortest(int a, int b, vector<Node> &aNodeCol, const int IsTime){
	aNodeCol[a].IsSelected = true;
	int TempMin = 2147483647/2;
	int aNode;
	//Node fad;
	for (unsigned int i = 0; i < aNodeCol.size(); ++i){
		if (aNodeCol[i].IsSelected){
			for (unsigned int j = 0; j < aNodeCol[i].ToCollection.size(); ++j){
				if (!(*(aNodeCol[i].ToCollection[j].To)).IsSelected){
					if (IsTime){
						if (TempMin>aNodeCol[i].Lowest + aNodeCol[i].ToCollection[j].Time){
							TempMin = aNodeCol[i].Lowest + aNodeCol[i].ToCollection[j].Time;
							aNode = (*(aNodeCol[i].ToCollection[j].To)).aa;
							//fad = *aNodeCol[i].ToCollection[j].To;
						}
					}
					else{
						if (TempMin>aNodeCol[i].Lowest + aNodeCol[i].ToCollection[j].Cost){
							TempMin = aNodeCol[i].Lowest + aNodeCol[i].ToCollection[j].Cost;
							aNode = (*(aNodeCol[i].ToCollection[j].To)).aa;
							//fad = *aNodeCol[i].ToCollection[j].To;
						}
					}
				}
			}
		}
	}
	if (b != aNode){
		aNodeCol[aNode].Lowest = TempMin;
		return GetShortest(aNode, b, aNodeCol, IsTime);
	}
	/*if (GoalNode != fad){
		aNodeCol[aNode].Lowest = TempMin;
		return GetShortest(aNode, GoalNode, b, aNodeCol, IsTime);
	}*/
	return TempMin;
}

int main(){
	int n, m;
	cin >> n >> m;
	vector<Node> NodeCollection;
	for (int i = 0; i < m; ++i){
		Node aNode;
		aNode.aa = i;
		NodeCollection.push_back(aNode);
	}

	for (int i = 0; i < n; ++i){
		int a, b, cost, time;
		cin >> a >> b >> cost >> time;
		NodeCollection[a - 1].ToCollection.push_back({ &NodeCollection[b - 1], cost, time });
		NodeCollection[b - 1].ToCollection.push_back({ &NodeCollection[a - 1], cost, time });
	}
	int k;
	cin >> k;
	for (int i = 0; i < k; ++i){
		int p, q, r;
		cin >> p >> q >> r;
		NodeCollection[p - 1].Lowest = 0;
		cout << GetShortest(p - 1, q - 1, NodeCollection, r) << endl;
		for (auto i = NodeCollection.begin(); i != NodeCollection.end(); ++i){
			(*i).Reset();
		}
	}
	cin >> k >> k;
	return 0;
}