#include <iostream>
#include <vector>
#include <queue>
#include <algorithm>

#define V_MAX 100
using namespace std;

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

vector<edge> money_g[V_MAX];
vector<edge> time_g[V_MAX];

int money_d[V_MAX], time_d[V_MAX];

#define INF 1000000

typedef pair<int, int> P; // cost, to
int min_money_cost(int from, int to) {
    priority_queue<P, vector<P>, greater<P> > q;
    fill(money_d, money_d+V_MAX, INF);
    money_d[from] = 0;
    q.push(P(0, from));

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

    return money_d[to];
}

int min_time_cost(int from, int to) {
    priority_queue<P, vector<P>, greater<P> > q;
    fill(time_d, time_d+V_MAX, INF);
    time_d[from] = 0;
    q.push(P(0, from));

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

    return time_d[to];
}

void init() {
    for (int i = 0; i < V_MAX; i++) {
        time_g[i].clear();
        money_g[i].clear();
    }
}

int main() {
    int n, m;
    while (cin >> n >> m, (n || m)) {
        init();
        for (int i = 0; i < n; i++) {
            int a, b, money, time;
            cin >> a >> b >> money >> time;
            money_g[a].push_back(edge(b, money));
            money_g[b].push_back(edge(a, money));
            time_g[a].push_back(edge(b, time));
            time_g[b].push_back(edge(a, time));
        }
        int k; cin >> k;
        for (int i = 0; i < k; i++) {
            int from, to, query_t;
            cin >> from >> to >> query_t;
            int res;
            if (query_t == 0) res = min_money_cost(from, to);
            else res = min_time_cost(from, to);
            cout << res << endl;
        }
    }
    return 0;
}