#include <algorithm>
#include <iostream>
#include <vector>
#include <queue>
#include <tuple>
using namespace std;

template <typename T>
struct Edge { int src, dst; T cost;
    Edge(int dst, T cost) : src(-1), dst(dst), cost(cost) { }
    Edge(int src, int dst, T cost) : src(src), dst(dst), cost(cost) { }
};
template <typename T> using Edges = vector<Edge<T>>;
template <typename T> using WeightedGraph = vector<Edges<T>>;
template <typename T> using Matrix = vector<vector<T>>;

template <typename T>
void warshall_floyd(Matrix<T> &g) {
    const T INF = numeric_limits<T>::max();
    for (int k = 0; k < g.size(); k++) {
        for (int i = 0; i < g.size(); i++) if (g[i][k] != INF) {
            for (int j = 0; j < g.size(); j++) if (g[k][j] != INF) {
                g[i][j] = min(g[i][j], g[i][k] + g[k][j]);
            }
        }
    }
}

int main() {
    int n, m;
    const auto INF = numeric_limits<int>::max();
    while (cin >> n >> m, n) {
        Matrix<int> cost(m, vector<int>(m, INF));
        Matrix<int> time(m, vector<int>(m, INF));
        for (int i = 0; i < m; i++) cost[i][i] = 0, time[i][i] = 0;
        while (n--) {
            int a, b, c, t; cin >> a >> b >> c >> t; a--, b--;
            cost[a][b] = cost[b][a] = c;
            time[a][b] = time[b][a] = t;
        }
        warshall_floyd(cost);
        warshall_floyd(time);
        int k; cin >> k;
        while (k--) {
            int p, q, r; cin >> p >> q >> r; p--, q--;
            cout << (r == 0 ? cost[p][q] : time[p][q]) << endl;
        }
    }
    return 0;
}
