//#include "bipartite-graph.hpp"
//#include <vector>
//using namespace std;
//int main(){
//	vector<vector<int> > G(4);
//	cout << bipartite_graph(G) << endl;
//}

// AOJ0200
#include <iostream>
#include <vector>
using namespace std;

struct edge {
    edge(){}
    edge(int to, int from, int cost, int time): to(to), from(from), cost(cost), time(time) {}
    int to, from, cost, time;
};

const int INF = 100000000;

int d[101];
edge es[7000];
int n,m;

// 頂点sから各頂点への最短経路を求める
void bellman_ford(int s, bool r){
    for (int i = 0; i < m; ++i) d[i]=INF;
    d[s]=0;
    while(1){
        bool update=false;
        for (int i = 0; i < n*2; ++i) {
            edge e=es[i];
            int cost;
            if(r) cost=e.time;
            else cost=e.cost;
            if(d[e.from]!=INF && d[e.to]>d[e.from]+cost){
                d[e.to]=d[e.from]+cost;
                update=true;
            }
        }
        if(!update) break;
    }
}

int main(){
    while(cin >> n >> m,n+m){
        for (int i = 0; i < n; ++i) {
            int to,from,cost,time;
            cin >> to>>from>>cost>>time;
            es[i]=edge(to-1,from-1,cost,time);
            es[n+i]=edge(from-1,to-1,cost,time);
        }
        int k; cin>>k;
        for (int i = 0; i < k; ++i) {
            int p,q,r;
            cin>>p>>q>>r;
            bellman_ford(p-1,r);
            cout<<d[q-1]<<endl;
        }
    }
}