#include <iostream>
#include <vector>
#include <algorithm>
#include <queue>
#define FOR(i,a,b) for(int i=(a); i<(b); i++)
#define SORT(c) sort((c).begin(), (c).end())
#define pb push_back
using namespace std;

const int INF=10000000;
int V;
struct edge {
    int to,money, time;
};
struct state {
    int from, d;
    bool operator<(const state& a)const{
        return d>a.d;
    }
};
vector<vector<edge> > G;

int d[111];
int ds(int s,int g, int r){
    FOR(i,0,111)d[i]=INF;
    d[s]=0;
    priority_queue<state> q;
    q.push({s,0});
    while(q.size()){
        state sta=q.top();
        q.pop();
        int from=sta.from;
        FOR(i,0,G[from].size()){
            const edge& e=G[from][i];
            int to=e.to;
            int cost=r?e.time:e.money;
            if(d[to]>d[from]+cost){
                d[to]=d[from]+cost;
                q.push({to,d[to]});
            }
        }
    }
    return d[g];
}

int main(){
    int n;
    while(1){
        cin>>n>>V;
        if(!(n||V)) break;
        G=vector<vector<edge> >(n+1);
        FOR(i,0,n){
            int a,b,m,t;cin>>a>>b>>m>>t;
            G[a].pb({b,m,t});
            G[b].pb({a,m,t});
        }
        int k;cin>>k;
        FOR(i,0,k){
            int p,q,r;cin>>p>>q>>r;
            cout<<ds(p,q,r)<<endl;
        }
    }
}