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

struct tEdge{
	short to, val;
	tEdge( short t, short v ){
		to = t;		val = v;
	}
};

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

int main(){
	vector< vector<short> > cost, time;
	priority_queue< tEdge >	edges;
	vector<bool> checked;
	int n, m;
	while( cin >> n >> m, n||m ){
		cost.resize(m+1);
		time.resize(m+1);
		checked.resize(m+1);
		for( int i=1; 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;
			}
		}
		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;
			if( p==q ){	cout << 0 << endl;	continue;	}
			while( !edges.empty() )	edges.pop();
			for( int j=1; j<=m; j++ ){
				if( r==0 && cost[p][j]!=-1 )		edges.push( tEdge( j, cost[p][j] ) );
				else if( r==1 && time[p][j]!=-1 )	edges.push( tEdge( j, time[p][j] ) );
				checked[j] = false;
			}
			checked[p] = true;
			while(1){
				tEdge e(0,0);
				do{
					e = edges.top();
					edges.pop();
				}while( checked[e.to] );
				if( e.to == q ){
					cout << e.val << endl;
					break;
				}
				checked[e.to] = true;
				for( int x=1; x<=m; x++ ){
					if( !checked[x] ){
						if( r==0 && cost[e.to][x]!=-1 ){
							edges.push( tEdge( x, e.val+cost[e.to][x] ) );
						}else if( r==1 && time[e.to][x]!=-1 ){
							edges.push( tEdge( x, e.val+time[e.to][x] ) );
						}
					}
				}
			}
		}
	}	
	return 0;
}