#include<cstdio>
#include<iostream>
#include<cstring>
#include<algorithm>
using namespace std;
#define INF 99999
int main(){
	while(1){
		int n,m;
		scanf("%d%d",&n,&m);
		if(n == 0 && m == 0)break;
		int D[128][128][2]={{{0}}};/*Adjacency matrix*/
		for(int i = 1; i <= n; i++){
			for(int j = 1; j <= n; j++){
				if(i == j){
					D[i][j][0] = 0;
					D[i][j][1] = 0;
					continue;
				}
				D[i][j][0] = INF;
				D[i][j][1] = INF;
			}
		}
		
		for(int i = 0; i < n; i++){
			int from,to,cst,tm;
			scanf("%d%d%d%d",&from,&to,&cst,&tm);
			D[from][to][0] = D[to][from][0] = cst;
			D[from][to][1] = D[to][from][1] = tm;
		}
		
		for(int l = 0; l < 2; l++){
			for(int k = 1; k <= n; k++){
				for(int i = 1; i <= n; i++){
					for(int j = 1; j <= n; j++){
						D[i][j][l] = min(D[i][j][l],D[i][k][l] + D[k][j][l]);
					}
				}
			}
		}
		
		int k;
		scanf("%d",&k);
		for(int i = 0; i < k; i++){
			int from,to,x;
			scanf("%d%d%d",&from,&to,&x);
			printf("%d\n",D[from][to][x]);
		}
	}
	return 0;
}