#include <cstdio>
#include <algorithm>

using namespace std;

int main()
{
	int n, m;
	int wf[128][128][2];
	
	while (scanf("%d %d", &n, &m) && n){
		for (int i = 0; i < 128; i++){
			for (int j = 0; j < 128; j++){
				wf[i][j][0] = 100000000 * (i != j);
				wf[i][j][1] = 100000000 * (i != j);
			}
		}
		
		for (int i = 0; i < n; i++){
			int a, b, cost, time;
			scanf("%d %d %d %d", &a, &b, &cost, &time);
			wf[a][b][0] = wf[b][a][0] = min(wf[a][b][0], cost);
			wf[a][b][1] = wf[b][a][1] = min(wf[a][b][1], time);
		}
		
		for (int k = 0; k < 128; k++){
			for (int i = 0; i < 128; i++){
				for (int j = 0; j < 128; j++){
					for (int l = 0; l < 2; l++){
						wf[i][j][l] = min(wf[i][j][l], wf[i][k][l] + wf[k][j][l]);
					}
				}
			}
		}
		
		int q;
		scanf("%d", &q);
		
		for (int i = 0; i < q; i++){
			int a, b, c;
			scanf("%d %d %d", &a, &b, &c);
			printf("%d\n", wf[a][b][c]);
		}
	}
	return (0);
}