#include <iostream>
#include <cmath>
#include <cstring>
#define FOR(i,a,b) for(int i=(a); i<(b); i++)
#define pb push_back
using namespace std;

const int INF=10000000;
int V;
int d[2][111][111];

void wf(){
    FOR(k,0,V+1)FOR(i,0,V+1)FOR(j,0,V+1){
        d[0][i][j]=min(d[0][i][j],d[0][i][k]+d[0][k][j]);
        d[1][i][j]=min(d[1][i][j],d[1][i][k]+d[1][k][j]);
    }
}

int main(){
    int n;
    while(1){
        cin>>n>>V;
        if(!(n||V)) break;
        FOR(i,0,V+1)FOR(j,0,V+1){
            d[0][i][j]=d[1][j][i]=INF;
        }
        FOR(i,0,n){
            int a,b,m,t;cin>>a>>b>>m>>t;
            d[0][a][b]=d[0][b][a]=t;
            d[1][a][b]=d[1][b][a]=m;
        }
        wf();
        int k;cin>>k;
        FOR(i,0,k){
            int p,q,r;cin>>p>>q>>r;
            cout<<d[r][p][q]<<endl;
        }
    }
}