#include<cstdio>
#include<algorithm>
#include<cstdlib>
#include<vector>
#include<string>
#include<cmath>

using namespace std;
#define rep(i,j) for(int i=0;i<(j);i++)
int ans[2][100][100];
int n,m;
int main(void){
    for(;;){
        scanf("%d%d",&n,&m);
        if(!n && !m)break;
        rep(i,2)rep(j,100)rep(k,100)if(j!=k)ans[i][j][k] = 99999999;else ans[i][j][k]=0;
        rep(i,n){
            int a,b,c,t;
            scanf("%d%d%d%d",&a,&b,&c,&t);
            a--;
            b--;
            ans[1][a][b] = t;
            ans[1][b][a] = t;
            ans[0][a][b] = c;
            ans[0][b][a] = c;
        }
        rep(k,m)rep(i,m)rep(j,m)rep(l,2)ans[l][i][j]=min(ans[l][i][j],ans[l][i][k]+ans[l][k][j]);
        int k,p,q,r;
        scanf("%d\n",&k);
        rep(i,k){
            scanf("%d%d%d",&p,&q,&r);
            q--;
            p--;
            printf("%d\n",ans[r][p][q]);
        }
    }

	return 0;
}