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

struct edge {
    int to,from,money,time;
};

vector<edge> E;
int V;

int d[111];

int wf(int p, int q, bool r){
    FOR(i,0,111)d[i]=100000000;
    d[p]=0;
    while(1){
        bool update=0;
        FOR(i,0,E.size()){
            edge e=E[i];
            int cost=r?e.time:e.money;
            if(d[e.to]>cost+d[e.from]){
                d[e.to]=cost+d[e.from];
                update=1;
            }
        }
        if(!update)break;
    }
    return d[q];
}

int main(){
    int n;
    while(1){
        E.clear();
        cin>>n>>V;
        if(n==0&&V==0) break;
        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<<wf(p,q,r)<<endl;
        }
    }
}