#include <stdio.h>
#include <cstdio>
#include <iostream>
#include <string>
#include <string.h>
#include <algorithm>
#include <math.h>
#include <vector>

using namespace std;

#define MAX_N 3000
#define MAX_M 100
#define MAX_K 200

int N, M, K;
//int P, Q, R;
int cost[MAX_M][MAX_M];
int times[MAX_M][MAX_M];
const int inf = 100100;

//R 0:最小金額、 1:最短時間

void init()
{
	for(int y=1; y<=M; ++y)
	{
		for(int x=1; x<=M; ++x)
		{
			cost[x][y] = inf;
			times[x][y] = inf;
		}
	}
}

void show(int R)
{
	auto weight = (R? times : cost);
	for(int x=1; x<=M; ++x)
	{
		for(int y=1; y<=M; ++y)
		{
			if(weight[x][y] == inf){cout << "inf ";}
			else{printf("%3d ",weight[x][y]);}
		}
		cout << endl;
	}
}

void floyd_warshall(int P, int Q, int R)
{
	auto weight = (R? times : cost);
	for(int k=1; k<=M; ++k)
	{
		for(int i=1; i<=M; ++i)
		{
			for(int j=1; j<=M; ++j)
			{
				if(weight[i][j] > weight[i][k]+weight[k][j])
				{
					weight[i][j] = weight[i][k]+weight[k][j];
				}
			}
		}
	}
	cout << weight[P][Q] << endl;
}

void dijkstra()
{
	//for(int i=0; )
}

void bellman_ford(int P, int Q, int R)
{
	auto weight = (R? times : cost);
	
	int D[M+1];
	for(int i=0; i<=M; ++i)
	{
		D[i] = inf;
	}
	D[P] = 0;
	for(int i=0; i<abs(M)-1; ++i)
	{
		for(int u=1; u<=M; ++u)
		{
			for(int v=1; v<=M; ++v)
			{
				D[v] = min(D[v], D[u]+weight[u][v]);
			}
		}
	}
	cout << D[Q] << endl;
}

int main()
{
	cin >> N >> M;
	init();
	int a, b, c, t;
	for(int i=0; i<N; ++i)
	{
		cin >> a >> b >> c >> t;
		cost[a][b] = c;
		cost[b][a] = c;
		times[a][b] = t;
		times[b][a] = t;
	}
	cin >> K;
	
	int P, Q, R;
	for(int i=0; i<K; ++i)
	{
		cin >> P >> Q >> R;
		bellman_ford(P,Q,R);
		//floyd_warshall(P, Q, R);
	}
	
}