#include <iostream>

using namespace std;

void search(int,int,int);

int	n,m,**lns[2];
unsigned int *val;
bool	*uvtd;

int main(void){
	int	K;

	do{
		cin >> n >> m;

		if(n!=0||m!=0){
			for(int i=0;i<2;i++){
				lns[i] = new int*[m+1];
				for(int j=1;j<=m;j++){
					lns[i][j] = new int[n];
					for(int k=0;k<n;k++)
						lns[i][j][k]=0;
				}
			}
			val = new unsigned int[m+1];
			uvtd = new bool[m+1];

			for(int i=0;i<n;i++){
				int a,b,cost,time;
				cin >> a >> b >> cost >> time;
				lns[0][a][i]=lns[0][b][i]=cost;
				lns[1][a][i]=lns[1][b][i]=time;
			}

			cin >> K;
			for(int i=0;i<K;i++){
				int p,q,r;
				cin >> p >> q >> r;
				for(int j=1;j<=m;j++){
					val[j]=-1;
					uvtd[j]=true;
				}
				val[p]=0;
				search(p,q,r);
				cout << val[q] << endl;
			}

			for(int i=0;i<2;i++){
				for(int j=1;j<=m;j++)
					delete [] lns[i][j];
				delete [] lns[i];
			}
			delete [] val;
			delete [] uvtd;
		}
	}while(n!=0||m!=0);
	return 0;
}

void search(int sta,int fin,int mode){
	uvtd[sta]=false;
	for(int i=0;i<n;i++)
		if(lns[mode][sta][i]){
			for(int j=1;j<=m;j++)
				if(uvtd[j]&&lns[mode][j][i]){
					if(val[sta]+lns[mode][sta][i]<val[j])
						val[j]=val[sta]+lns[mode][sta][i];
					if(j!=fin){
						search(j,fin,mode);
						uvtd[j]=true;
					}
				}
		}

}