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

const int INF = 1 << 29;
const int M_MAX = 100;
int m;
int ccs[M_MAX+1][M_MAX+1];
int tcs[M_MAX+1][M_MAX+1];

void floyd() {
    for (int k = 1; k <= m; k++) {
        for (int i = 1; i <= m; i++) {
            for (int j = 1; j <= m; j++) {
                ccs[i][j] = min(ccs[i][j], ccs[i][k] + ccs[k][j]);
                tcs[i][j] = min(tcs[i][j], tcs[i][k] + tcs[k][j]);
            }
        }
    }
}

int main() {
    int n;
    while (cin >> n >> m, n|m) {
        for (int i = 1; i <= m; i++) {
            for (int j = 1; j <= m; j++) {
                ccs[i][j] = tcs[i][j] = (i == j) ? 0 : INF;
            }
        }
        for (int i = 0; i < n; i++) {
            int a, b, c, t;
            cin >> a >> b >> c >> t;
            ccs[a][b] = ccs[b][a] = c;
            tcs[a][b] = tcs[b][a] = t;
        }
        floyd();
        int k;
        cin >> k;
        for (int i = 0; i < k; i++) {
            int p, q, r;
            cin >> p >> q >> r;
            if (r == 0) {
                cout << ccs[p][q] << endl;
            } else {
                cout << tcs[p][q] << endl;
            }
        }
    }
}