#include <iostream>
using namespace std;
const int MAX = 100,INF = 1000000;
int edge[MAX + 1][MAX + 1][2],node[MAX + 1];
bool check[MAX + 1];

int f(int a,int b,int c,int m){
	int t,x;
	for(int i = 0;i <= MAX;++i){
		check[i] = false;
		node[i] = INF;
	}
	node[a] = 0;
	while(!check[b]){
		x = INF;
		for(int i = 1;i <= m;++i){
			if(!check[i] && node[i] < x) {
				x = node[i];
				t = i;
			}
		}
		check[t] = true;
		for(int i = 0;i <= m;++i){
			if(node[i] > node[t] + edge[t][i][c]){
				node[i] = node[t] + edge[t][i][c];
			}
		} 
	}
	return node[b];
}
int main(){
	int n,m,a,b,c,d;
	while(cin >> n >> m,n){
		for(int i = 0;i <= MAX;++i){
			for(int j = 0;j <= MAX;++j){
				edge[i][j][0] = edge[j][i][0] = INF;
				edge[i][j][1] = edge[j][i][1] = INF;
			}
		}
		for(int i = 0;i < n;++i){
			cin >> a >> b >> c >> d;
			edge[a][b][0] = edge[b][a][0] = c;
			edge[a][b][1] = edge[b][a][1] = d;
		}
		cin >> n;
		for(int i = 0;i< n;++i){
			cin >> a >> b >> c;
			cout << f(a,b,c,m) << endl;
		}
	}
}