/*

?????????????????? ????????\?????????????¬???¨???

??? ???
???????????´??????????´\????????????????????????????????§????????????????¨??????????????????????????????????
????????????????????§??????????????????????????????????????????????????§????????????????????????????????????
??????????????\??????????????????????????¨?????\???????????????????????????????????????????????????????????°
??????????¨????????????????????????????????????????????´?????????????????????§??????????????????????????????
?¨??????????????????????????????°??????????????£????????????????????????

??????????????±????§??????°?????\?????¨?????????????????????????????????????????????????????????????°???????
???????????????????????°????????????????????????????????????????§???? 1 ??\??? 1000 ??\????????´??°??§
??¨??????????§??????° m ??? 1 ??\??? 1000 ??\?????§????????????????????±??????????????????????????????
????§???? a b ???????§?????????? time ????????? cost ??§??¨????????? time cost ??????????????? 
1 ??\??? 1000 ??\?????¨????????????????????????????????????????§? p ?????°????§? q ???????¨???? r 
??§??¨????????? r ??? 0 ?????¨???????????????????????? 1 ?????????????°???????????????????????????????
?????????????????° k ??? 1 ??\??? 1000 ??\?????§??????????????????????????????????????????????????????
???????????¨????????????

??\ ???
?????°???????????????????????????????????\?????¨?????????????????????????????\??????????????????????????????
???????????§????????????????????????????????????????????\????????¨????????§??????
1 ?????? ??????????????±?????° n????§??????° m ?????´??° ??´??°???????§???????????????????
2 ?????? ?¬¬ 1 ?????????????????± a b cost time?????´??° ??´??° ??´??° ??´??°???????§???????????????????
3 ?????? ?¬¬ 2 ?????????????????±
???
n+1 ?????? ?¬¬ n ?????????????????±
n+2 ?????? ????????????????????° k?????´??°???
n+3 ?????? ?¬¬ 1 ?????????????????? p q r ?????´??° ??´??° ??´??°???????§???????????????????
n+4 ?????? ?¬¬ 2 ??????????????????
 ???
n+2+k ??? ?¬¬ k ??????????????????


??? ???
??\??????????????????????????¨??????????????????????????????????°????????????????????????????

*/

/*
	http://judge.u-aizu.ac.jp/onlinejudge/review.jsp?rid=552375#1
 */

#include <stdio.h>
 
#define MAX_STATION (100+1)
#define MAX 1000000000
#define min(x, y) ((x) < (y) ? (x) : (y))
 
int croute[ MAX_STATION ][ MAX_STATION ];
int troute[ MAX_STATION ][ MAX_STATION ];
int cost[ MAX_STATION ];
int check[ MAX_STATION ];
int n, m;

void dijkstra(int start, int route[ MAX_STATION ][ MAX_STATION ] )
{
	int i, u;

	for ( i = 1; i < m + 1; i++ ) {
		cost[ i ] = MAX;
		check[ i ] = 0;
	}
	cost[ start ] =0;
	while (1) {
		// ???????¢???????????????????,???????????????????°?????????¨????????¢???
		int v = -1;
		for ( u = 1; u < m + 1; u++ ) {
			if ( check[ u ] == 0 && ( v == -1 || cost[ u ] < cost[ v ] ) )
				v = u;
		}
		if (v == -1) {
			break;
		}
		check[v] = 1;

		// v ????????±??????????????§cost?????´??°??§??????????????????
		for ( u = 1 ; u < m + 1 ; u++ ) {
			cost[ u ] = min( cost[ u ], cost[ v ] + route[ v ][ u ] );
		}
	}
}
 
int main( void )
{
	int i, j, k;
	int result;

	while ( scanf( "%d %d", &n, &m ), n != 0 ) {
		for ( i = 0 ; i < MAX_STATION ; i++ ) {
			for (j = 0 ; j < MAX_STATION ; j++ ) {
				croute[ i ][ j ] = ( i == j ) ? 0 : MAX;
				troute[ i ][ j ] = ( i == j ) ? 0 : MAX;
			}
		}
		while (n > 0) {
			int a, b, c, t;
			scanf( "%d %d %d %d", &a, &b, &c, &t );
			croute[ a ][ b ] = croute[ b ][ a ] = c;
			troute[ a ][ b ] = troute[ b ][ a ] = t;
			n--;
		}
		
		scanf( "%d", &k );
		while ( k > 0 ) {
			int p, q, r;
			scanf( "%d %d %d", &p, &q, &r );
			dijkstra( p, ( r == 0 ) ? croute : troute );
			result = cost[ q ];
			printf( "%d\n", result );
			k--;
		}
	}
	return 0;
}

Measured memory_bytes: 680