#include<string>
#include<vector>
#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cstdlib>
#include<sstream>
#include<functional>
#include<map>
#include<set>
#include<cmath>

#define REP(i,s,e) for(int i=int(s);i<=int(e);i++)
#define rep(i,n) for(int i=0;i<int(n);i++)

using namespace std;

#define INF 10000

int m,x,y;
int c[105][105],ct[105][105];
int d[105],dt[105];
bool b[105],bt[105];

void mc(int s){
rep(i,m){
d[i]=INF; b[i]=false;
}
d[s]=0;

while(1){
int M=INF;
rep(i,m){
if(!b[i] && d[i]<M){
	M=d[i]; x=i;
}
}

if(M==INF) break;

b[x]=true;
rep(i,m) d[i]=min(d[i],d[x]+c[x][i]);

}
}

void mt(int s){
rep(i,m){
dt[i]=INF; bt[i]=false;
}
dt[s]=0;

while(1){
int M=INF;
rep(i,m){
if(!bt[i] && dt[i]<M){
	M=dt[i]; y=i;
}
}

if(M==INF) break;

bt[y]=true;
rep(i,m) dt[i]=min(dt[i],dt[y]+ct[y][i]);

}
}

int main(){

rep(i,105){
rep(j,105){
	c[i][j]=INF;
	ct[i][j]=INF;
}
}

int n,a,b,cost,time,k,p,q,r;

while(1){

cin >> n >> m;
if(n==0 && m==0) break;

rep(i,n){
cin >> a >> b >> cost >> time;

c[a-1][b-1]=cost; c[b-1][a-1]=cost;
ct[a-1][b-1]=time; ct[b-1][a-1]=time;
}

cin >> k;

rep(i,k){
cin >> p >> q >> r;

if(r==0){
	mc(p-1); cout << d[q-1] << endl;


}

else{
	mt(p-1); cout << dt[q-1] << endl;


}
}


}


	return 0;
}