#include <iostream>
#include <queue>
#include <vector>
#include <algorithm>
using namespace std;

#define MAX_E 300
#define MAX_V 100
#define INF 1 << 28

struct edge {
    int to, cost;
    edge(int to, int cost) : to(to), cost(cost) {}
    void operator=(const edge& o) {
        to = o.to;
        cost = o.cost;
    }
};

typedef pair<int, int> P;

vector<edge> GTime[MAX_V];
int hour[MAX_V];
vector<edge> GMoney[MAX_V];
int money[MAX_V];
int V, E;

void shortest_path(int s) {
    priority_queue< P, vector<P>, greater<P> > q;
    fill(hour, hour+V, INF);
    fill(money, money+V, INF);
    hour[s] = 0;
    money[s] = 0;

    q.push(P(0, s));
    while (!q.empty()) {
        P p = q.top(); q.pop();
        int v = p.second;
        if (hour[v] < p.first) continue;
        for (int i = 0; i < GTime[v].size(); i++) {
            edge e = GTime[v][i];
            if (hour[e.to] > hour[v] + e.cost) {
                hour[e.to] = hour[v] + e.cost;
                q.push(P(hour[e.to], e.to));
            }
        }
    }

    q.push(P(0, s));
    while (!q.empty()) {
        P p = q.top(); q.pop();
        int v = p.second;
        if (money[v] < p.first) continue;
        for (int i = 0; i < GMoney[v].size(); i++) {
            edge e = GMoney[v][i];
            if (money[e.to] > money[v] + e.cost) {
                money[e.to] = money[v] + e.cost;
                q.push(P(money[e.to], e.to));
            }
        }
    }
}

int main() {
    while (cin >> E >> V) {
        if (E == 0 && V == 0) break;
        for (int i = 0; i < MAX_V; i++) {
            GTime[i].clear();
            GMoney[i].clear();
        }
        for (int i = 0; i < E; i++) {
            int a, b, _money, _hour;
            cin >> a >> b >> _money >> _hour;
            GTime[a-1].push_back(edge(b-1, _hour));
            GTime[b-1].push_back(edge(a-1, _hour));
            GMoney[a-1].push_back(edge(b-1, _money));
            GMoney[b-1].push_back(edge(a-1, _money));
        }
        int k; cin >> k;
        for (int i = 0; i < k; i++) {
            int from, to, qtype;
            cin >> from >> to >> qtype;
            shortest_path(from-1);
    //        for (int i = 0; i < V; i++) {
    //            cout << money[i] << ' ';
    //        } cout << endl;
    //        for (int i = 0; i < V; i++) {
    //            cout << hour[i] << ' ';
    //        } cout << endl;
    
            if (qtype == 0) {
                cout << money[to-1] << endl;
            } else {
                cout << hour[to-1] << endl;
            }
        }
    }
    return 0;
}