#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <cstring>
using namespace std;

#define REP(i,k,n) for(int i=k;i<n;++i)
#define rep(i,n) REP(i,0,n)

int N,M,K;
const int MAXN=128;
const int INF=1e9;
int G[2][MAXN][MAXN];

int dk(int from,int to,int type)
{
	bool ok[MAXN];
	int cost[MAXN];
	memset(ok,0,sizeof(ok));
	memset(cost,0,sizeof(cost));
	ok[from]=true;

	while(!ok[to])
	{
		int t,v=INF; 
		rep(i,M) if(ok[i]) rep(j,M) if(!ok[j]&&G[type][i][j])
		{
			if(cost[i]+G[type][i][j]<v){ v=cost[i]+G[type][i][j]; t=j; }
		}

		if(v==INF){ cout<<"ERROR"<<endl; break; } else { cost[t]=v; ok[t]=true; }
	}

	return cost[to];
}

int main()
{
	cin>>N>>M;
	int a,b,c,d;
	
	rep(i,N)
	{ 
		cin>>a>>b>>c>>d; --a; --b;
		G[0][a][b]=G[0][b][a]=c;
		G[1][a][b]=G[1][b][a]=d;
	}

	cin>>K;
	rep(i,K)
	{
		cin>>a>>b>>c; --a; --b;
		cout<<dk(a,b,c)<<endl;
	}


	return 0;
}