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

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

int main() {
    int n, m, a, b, v, u, k, p, q, r;
    while (cin >> n >> m, n || m) {
        vector<edge> edges;
        for (int i=0; i<n; ++i) {
            cin >> a >> b >> v >> u;
            edges.push_back((edge){ a, b, v, u });
        }
        cin >> k;
        for (int i=0; i<k; ++i) {
            cin >> p >> q >> r;
            int d[100];
            for (int i=0; i<100; ++i) d[i] = 1e8;
            d[p] = 0;
            for (int i=0; i<100; ++i) {
                for (int j=0; j<(int)edges.size(); ++j) {
                    edge &e = edges[j];
                    if (r == 0) {
                        d[e.to] = min(d[e.to], d[e.from] + e.cost);
                    } else {
                        d[e.to] = min(d[e.to], d[e.from] + e.time);
                    }
                }
            }
            cout << d[q] << endl;
        }
    }
    return 0;
}