#include <iostream>


#define MAX_M 110
#define INF   10000

int n, m, k;
int ct[MAX_M][MAX_M];
int tm[MAX_M][MAX_M];
int d[MAX_M][MAX_M];
int e[MAX_M][MAX_M];

void warshall_floyd() {
    for (int k=1; k<=m; k++) {
        for (int i=1; i<=m; i++) {
            for (int j=1; j<=m; j++) {
                ct[i][j] = std::min(ct[i][j], ct[i][k] + ct[k][j]);
                tm[i][j] = std::min(tm[i][j], tm[i][k] + tm[k][j]);
            }
        }
    }
}

int main() {
    while (true) {
        std::cin >> n >> m;
        if (!n && !m) break;
        for (int i=1; i<=m; i++) {
            for (int j=1; j<=m; j++) {
                ct[i][j] = INF;
                tm[i][j] = INF;
                if (i == j) {
                    ct[i][j] = 0;
                    tm[i][j] = 0;
                }
            }
        }

        for (int i=0; i<n; i++) {
            int a, b, c, t;
            std::cin >> a >> b >> c >> t;
            ct[a][b] = c;   ct[b][a] = c;
            tm[a][b] = t;   tm[b][a] = t;
        }
        warshall_floyd();

        std::cin >> k;
        for (int i=0; i<k; i++) {
            int p, q, r;
            std::cin >> p >> q >> r;
            if (r == 0) {
                std::cout << ct[p][q] << std::endl;
            } else {
                std::cout << tm[p][q] << std::endl;
            }
        }
    }
    return 0;
}

