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

const int INF = 10000000;
int cost[2][111][111];
int d[111];
bool used[111];
int V;
struct edge {
    int from,to,money,time;
};
vector<edge> E;

int bf(int s,int g,int r){
    FOR(i,0,111)d[i]=INF;
    d[s]=0;
    while(1){
        bool update=false;
        FOR(i,0,E.size()){
            const edge& e=E[i];
            int from=e.from;
            int to=e.to;
            int cost=r?e.time:e.money;
            if(d[to]>d[from]+cost){
                d[to]=d[from]+cost;
                update=true;
            }
        }
        if(!update)break;
    }
    return d[g];
}

int main(){
    int n;
    while(1){
        cin>>n>>V;
        if(!(n||V)) break;
        E.clear();
        FOR(i,0,n){
            int a,b,m,t;cin>>a>>b>>m>>t;
            E.pb({a,b,m,t});
            E.pb({b,a,m,t});
        }
        int k;cin>>k;
        FOR(i,0,k){
            int p,q,r;cin>>p>>q>>r;
            cout<<bf(p,q,r)<<endl;
        }
    }
}