#include <iostream>
#include <vector>
using namespace std;
#define REP(i,n) for (int i=0; i<(n); ++i)

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

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