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

class Node{
public:
	vector< int > e_to;
	vector< int > e_cost;
	
	int this_node;
	bool def;
	int min_cost;
	
	Node(){
		this_node = -1;
		def = false;
		min_cost = -1;
	}
};
bool operator>(const Node& ot1, const Node& ot2){
	return ot1.min_cost > ot2.min_cost;
}


class Dijkstra{
	priority_queue< Node, vector< Node >, greater< Node > > que;
	vector< Node > nodes;
	int node_num;
	
public:
	void Init(int n){
		node_num = n;
		nodes.resize(node_num);
		for(int i = 0; i < node_num; ++i){
			nodes[i] = Node();
		}
	}
	
	void Put(int id, int to, int cost){
			nodes[id].e_to.push_back(to);
			nodes[id].e_cost.push_back(cost);
			nodes[id].this_node = id;
	}
	
	int Run(int s, int t){
		
		for(vector< Node >::iterator it = nodes.begin(); it != nodes.end(); ++it){
			it->def = false;
			it->min_cost = -1;
		}
		
		nodes[s].min_cost = 0;
		que.push(nodes[s]);
		
		while(!que.empty()){
			Node def_node = que.top();
			que.pop();
		
			if(def_node.def)continue;
		
			def_node.def = true;
		
			for(int i = 0; i < def_node.e_to.size(); ++i){
				int to = def_node.e_to[i];
				int cost = def_node.min_cost + def_node.e_cost[i];
				
				if(nodes[to].min_cost == -1 || cost < nodes[to].min_cost){
					nodes[to].min_cost = cost;
					que.push(nodes[to]);
				}
			}
			
			if(def_node.this_node == t) return nodes[t].min_cost;
		}
		return nodes[t].min_cost;
	}
};

int main(){
	
	int n, m, a, b, c, t, k, p, q, r;
	Dijkstra dijk_cost, dijk_time;
	
	
	while(cin >> n >> m, n){
		dijk_cost.Init(m);
		dijk_time.Init(m);
		
		for(int i = 0; i < n; ++i){
			cin >> a >> b >> c >> t;
			
			dijk_cost.Put(a - 1, b - 1, c);
			dijk_cost.Put(b - 1, a - 1, c);
			
			dijk_time.Put(a - 1, b - 1, t);
			dijk_time.Put(b - 1, a - 1, t);
		}
		
		cin >> k;
		
		for(int i = 0; i < k; ++i){
			cin >> p >> q >> r;
			
			if(r == 0){
				cout << dijk_cost.Run(p - 1, q - 1) << endl;
			}else if(r == 1){
				cout << dijk_time.Run(p - 1, q - 1) << endl;
			}
		}
	}
	
	return 0;
}