#include <iostream>

const int INF = 1 << 29;

using namespace std;

int main(void){
	int n,m;
	
	while(cin >> n >> m){
		if((n|m) == 0) break;
		int dist[m][m];
		int time[m][m];
		
		for(int i=0;i<m;i++){
			for(int j=0;j<m;j++){
				if(i == j){
					dist[i][j] = 0;
					time[i][j] = 0;
				}else{
					dist[i][j] = INF;
					time[i][j] = INF;
				}
			}
		}
		
		int s, e, cd, ct;
		for(int i=0;i<n;i++){
			cin >> s >> e >> cd >> ct;
			dist[s-1][e-1] = cd;
			dist[e-1][s-1] = cd;
			time[s-1][e-1] = ct;
			time[e-1][s-1] = ct;
		}
		
		//WF
		for(int k=0;k<m;k++){
			for(int i=0;i<m;i++){
				for(int j=0;j<m;j++){
					dist[i][j] = min(dist[i][j], dist[i][k] + dist[k][j]);
					time[i][j] = min(time[i][j], time[i][k] + time[k][j]);
				}
			}
		}
		
		int cases,type;
		cin >> cases;
		for(int i=0;i<cases;i++){
			cin >> s >> e >> type;
			if(type == 0){
				cout << dist[s-1][e-1] << endl;
			}else{
				cout << time[s-1][e-1] << endl;
			}
		}
	}
}