#include<iostream>
#include<algorithm>
#include<queue>
#include<cstring>
using namespace std;

short cost[101][101];
short gtime[101][101];

struct tSta{
	int val;
	short sta, psta;
	tSta( int s ){
		val = 0;
		sta = s;
		psta = 0;
	}
};

bool operator<( const tSta& l, const tSta& r ){
	return l.val > r.val;
}

int main(){
	priority_queue<tSta> que;
	int n, m, k;
	while( cin >> n >> m, n||m ){
		memset( cost, 0, sizeof(cost) );
		memset( gtime, 0, sizeof(gtime) );
		for( int i=0; i<n; i++ ){
			int a, b, c, t;
			cin >> a >> b >> c >> t;
			cost[a][b] = cost[b][a] = c;
			gtime[a][b] = gtime[b][a] = t;
		}
		cin >> k;
		for( int i=0; i<k; i++ ){
			int p, q, r;
			cin >> p >> q >> r;
			while( !que.empty() )	que.pop();
			que.push( tSta(p) );
			while( !que.empty() ){
				tSta ts = que.top();
				if( ts.sta == q )	break;
				que.pop();
				if( r==0 ){	// cost
					for( int j=1; j<=m; j++ ){
						if( cost[ts.sta][j] > 0 && ts.psta != j ){
							tSta next(j);
							next.val = ts.val + cost[ts.sta][j];
							next.psta = ts.sta;
							que.push( next );
						}
					}
				}else if( r==1 ){	// time
					for( int j=1; j<=m; j++ ){
						if( gtime[ts.sta][j] > 0 && ts.psta != j ){
							tSta next(j);
							next.sta = j;
							next.val = ts.val + gtime[ts.sta][j];
							next.psta = ts.sta;
							que.push( next );
						}
					}
				}
			}
			cout << que.top().val << endl;
		}
	}
	return 0;
}