#include <iostream>
#include <vector>
#include <cstring>
#include <queue>

using namespace std;

#define loop(i,a,b) for(int i=(a); i<(int)(b); i++)
#define rep(i,b) loop(i,0,b)

struct edge{
    int to,money,time;
};
struct state{
    int v,d;
    bool operator<(const state& rhs)const{
        return d>rhs.d;
    }
};

priority_queue<state> q;
vector<vector<edge>> G;
int d[101];

int main(){
    int n,m;
    while(cin>>n>>m,n|m){
        G.resize(m+1);
        rep(i,n){
            int f,t,m,ti; cin>>f>>t>>m>>ti;
            G[f].push_back({t,m,ti});
            G[t].push_back({f,m,ti});
        }
        int k; cin>>k;
        rep(i,k){
            int a,b,c; cin>>a>>b>>c;
            priority_queue<state>().swap(q);
            for(int& i:d) i=1<<30;
            d[a]=0;
            q.push({a,0});
            while(q.size()){
                state sta=q.top(); q.pop();
                if(d[sta.v]<sta.d) continue;
                for(edge& e:G[sta.v]){
                    int cost=(c==0 ? e.money : e.time);
                    if(d[e.to]>d[sta.v]+cost){
                        d[e.to]=d[sta.v]+cost;
                        q.push({e.to,d[e.to]});
                    }
                }
            }
            cout<<d[b]<<endl;
        }
    }
}