#include <iostream>
#include <vector>
#include <string.h>
#include <time.h>

using namespace std;

static const int TYPE_COST =0;
static const int TYPE_TIME =1;

class station{
public:
	int cost;
	bool visited;
	vector<int> edge_to;
	vector<int> edge_cost;
	vector<int> edge_time;
	
	 public:
		station() :  cost(-1), visited(false){}
		void add_to(int to, int cost, int time)
		{
			this->edge_to.push_back(to);
			this->edge_cost.push_back(cost);
			this->edge_time.push_back(time);
		}
};

int dijkstra(station* sta, int size, int from, int to, int type);
void reset(station* sta, int size);

int main(int argc, char** argv)
{
	while( 1 ){
		int n, m, from, to, cost, time, type;
		cin >> n;
		cin >> m;
		if(n==0 && m==0) break;
		
		station sta[m];
		for(int i=0; i<m; i++){
			sta[i] = station();
		}
		
		for(int i=0; i<n; i++){
			cin >> from;
			cin >> to;
			cin >> cost;
			cin >> time;
			sta[from-1].add_to(to-1, cost, time);
			sta[to-1].add_to(from-1, cost, time);
		}
		cin >> n;
		for(int i=0; i<n; i++){
			cin >> from;
			cin >> to;
			cin >> type;
			clock_t t1 = clock();
			cout << dijkstra(sta, m, from-1, to-1, type) << endl;
			clock_t t2 = clock();
			cout << "Time : "  << t2 - t1<< " (msec)" << endl;
			reset(sta, m);
		}
	}
	
	return 0;
}

int dijkstra(station* sta, int size, int start, int goal, int type)
{
	int to, cost;
	station done;
	vector<station> q;
	sta[start].cost = 0;
	q.push_back(sta[start]);
	
	while(!q.empty()){
		done = q[0];
		q.erase(q.begin());
		done.visited = true;
		
		for(int i=0; i<done.edge_to.size(); i++){
			to = done.edge_to[i];
			if(type==TYPE_COST) cost = done.cost + done.edge_cost[i];
			if(type==TYPE_TIME) cost = done.cost + done.edge_time[i];
			if(sta[to].cost < 0 || cost < sta[to].cost){
				sta[to].cost = cost;
				q.push_back(sta[to]);
			}
		}
	}
	
	return sta[goal].cost;
}

void reset(station* sta, int size)
{
	for(int i=0; i<size; i++){
		sta[i].visited = false;
		sta[i].cost = -1;
	}
}