#include <stdio.h>
#include <string.h>
#include <queue>
#include <vector>
#include <algorithm>

using namespace std ;

struct Node
{
	int P ;
	int C ;
	Node ( ) { }
	Node ( int a , int b )
	{
		P = a ;
		C = b ;
	}
	bool operator < ( const Node &a ) const
	{
		return C > a.C ;
	}
} ;

vector < int > ListofRoad [110] ;
vector < int > ListofTime [110] ;
vector < int > ListofCost [110] ;

int CostAns [110][110] ;
int TimeAns [110][110] ;
bool vis[110] ;

void init ( ) ;
void Dijkstra ( int , int , int ) ;

int main ( )
{
	int n ;
	int m ;
	while ( scanf ( "%d %d" , &n , &m ) == 2 && ( n || m ) )
	{
		init ( ) ;
		for ( int i = 0 ; i < n ; i++ )
		{
			int a ;
			int b ;
			int c ;
			int d ;
			scanf ( "%d %d %d %d" , &a , &b , &c , &d ) ;
			ListofRoad[a].push_back ( b ) ;
			ListofCost[a].push_back ( c ) ;
			ListofTime[a].push_back ( d ) ;
			ListofRoad[b].push_back ( a ) ;
			ListofCost[b].push_back ( c ) ;
			ListofTime[b].push_back ( d ) ;
		}
		int Q ;
		scanf ( "%d" , &Q ) ;
		while ( Q-- )
		{
			int p ;
			int q ;
			int r ;
			scanf ( "%d %d %d" , &p , &q , &r ) ;
			if ( r == 0 )
			{
				if ( CostAns[p][q] == -1 )
				{
					Dijkstra ( p , m , 1 ) ;
				}
				printf ( "%d\n" , CostAns[p][q] ) ;
			}
			else
			{
				if ( TimeAns[p][q] == -1 )
				{
					Dijkstra ( p , m , 0 ) ;
				}
				printf ( "%d\n" , TimeAns[p][q] ) ;
			}
		}
	}
	return 0 ;
}

void init ( )
{
	for ( int i = 0 ; i < 110 ; i++ )
	{
		ListofRoad[i].clear ( ) ;
		ListofTime[i].clear ( ) ;
		ListofCost[i].clear ( ) ;
	}
	memset ( CostAns , -1 , 110 * 110 * sizeof ( int ) ) ;
	memset ( TimeAns , -1 , 110 * 110 * sizeof ( int ) ) ;
}

void Dijkstra ( int source , int m , int type )
{
	memset ( vis , false , 110 * sizeof ( bool ) ) ;
	priority_queue < struct Node > pQ ;
	if ( type == 0 )
	{
		TimeAns[source][source] = 0 ;
	}
	else
	{
		CostAns[source][source] = 0 ;
	}
	pQ.push ( Node ( source , 0 ) ) ;
	for ( int i = 1 ; i <= m ; i++ )
	{
		struct Node var = ( struct Node ) { -1 , -1 } ;
		while ( pQ.empty ( ) == false && vis[( var = pQ.top ( ) ).P] == true )
		{
			pQ.pop ( ) ;
		}
		if ( var.P == -1 )
		{
			break ;
		}
		vis[var.P] = true ;
		if ( type == 0 )
		{
			vector < int > :: iterator iu = ListofTime[var.P].begin ( ) ;
			for ( vector < int > :: iterator it = ListofRoad[var.P].begin ( ) ;
				it != ListofRoad[var.P].end ( ) ;
				it++ , iu++ )
			{
				if ( vis[*it] == false )
				{
					if ( TimeAns[source][*it] == -1 || ( TimeAns[source][*it] > var.C + *( iu ) ) )
					{
						TimeAns[source][*it] = var.C + *( iu ) ;
						pQ.push ( Node ( *it , TimeAns[source][*it] ) ) ;
					}
				}
			}
		}
		else
		{
			vector < int > :: iterator iu = ListofCost[var.P].begin ( ) ;
			for ( vector < int > :: iterator it = ListofRoad[var.P].begin ( ) ;
				it != ListofRoad[var.P].end ( ) ;
				it++ , iu++ )
			{
				if ( vis[*it] == false )
				{
					if ( CostAns[source][*it] == -1 || ( CostAns[source][*it] > var.C + *( iu ) ) )
					{
						CostAns[source][*it] = var.C + *( iu ) ;
						pQ.push ( Node ( *it , CostAns[source][*it] ) ) ;
					}
				}
			}
		}
	}
}