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

int main(){
	vector< vector<short> > cost, time;
	vector<int> station;
	int n, m;
	while( cin >> n >> m, n||m ){
		cost.resize(m+1);
		time.resize(m+1);
		station.resize(m+1);
		for( int i=0; i<m; i++ ){
			cost[i].resize(m+1);
			time[i].resize(m+1);
			for( int j=1; j<=m; j++ ){
				cost[i][j] = time[i][j] = -1;
			}
		}
		station.resize(m);
		for( int i=0; i<n; i++ ){
			int a, b, c, t;
			cin >> a >> b >> c >> t;
			cost[a][b] = cost[b][a] = c;
			time[a][b] = time[b][a] = t;
		}
		int k;
		cin >> k;
		for( int i=0; i<k; i++ ){
			int p, q, r;
			cin >> p >> q >> r;
			for( int j=1; j<=m; j++ ){
				station[j] = -1;
			}
			station[p] = 0;
			for( int z=0; z<m; z++ ){
				int ny=-1, nextVal=1000000;
				for( int x=1; x<=m; x++ ){
					if( station[x] != -1 ){
						for( int y=1; y<=m; y++ ){
							if( (station[y]==-1) && (cost[x][y] != -1) ){
								int val = station[x];
								if( r==0 )	// cost
									val += cost[x][y];
								else if( r==1 )	// time
									val += time[x][y];
								if( val < nextVal ){
									nextVal = val;
									ny = y;
								}
							}
						}
					}
				}
				station[ny] = nextVal;
				if( ny == q )	break;
			}
			cout << station[q] << endl;
		}
	}	
	return 0;
}