#include<iostream>
using namespace std;

#define INF (1000000000)
int main() {

	
	int n,m;
	while( cin >> n >> m && n){
		int twf[110][110],cwf[110][110];
		for ( int i = 0; i < 110; i++ ) {
			for ( int j = 0; j < 110; j++ ) {
				if ( i == j )
					twf[i][j] = cwf[i][j] = 0;
				else
					twf[i][j] = cwf[i][j] = INF;
			}
		}
		for ( int i = 0; i < n; i++ ) {
			int a,b,cost,time;
			cin >> a >> b >> cost >> time;
			a--;b--;
			twf[a][b] = time;
			twf[b][a] = time;
			cwf[a][b] = cost;
			cwf[b][a] = cost;
		}
		for ( int a = 0; a < m; a++ ) {
			for ( int b = 0; b < m; b++ ) {
				for ( int c = 0; c < m; c++ ) {
					twf[b][c] = min ( twf[b][c], twf[b][a] + twf[a][c] );
					cwf[b][c] = min ( cwf[b][c], cwf[b][a] + cwf[a][c] );
				}
			}
		}

		int k;
		cin >> k;
		for ( int i = 0; i < k; i++ ) {
			int p,q,r;
			cin >> p >> q >> r;
			p--,q--;
			if ( !r ) {
				cout << cwf[p][q] << endl;
			} else {
				cout << twf[p][q] << endl;
			}
		}
	}
}