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

struct Node {
    bool isDone;
    int cost;
};

const int MAX_M = 100;
int costTable[MAX_M + 1][MAX_M + 1];
int timeTable[MAX_M + 1][MAX_M + 1];
Node nodes[MAX_M + 1];
int m;

int dijkstra(int start, int end, int r) {
    int (*table)[MAX_M + 1];
    table = (r == 0) ? costTable : timeTable;
    for (int i = 1; i <= m; i++) {
        nodes[i].isDone = false;
        nodes[i].cost = -1;
    }
    nodes[start].cost = 0;
    while (true) {
        int min_idx = -1;
        for (int i = 1; i <= m; i++) {
            if (nodes[i].isDone == true || nodes[i].cost < 0) {
                continue;
            }
            if (min_idx < 0 || nodes[i].cost < nodes[min_idx].cost) {
                min_idx = i;
            }
        }
        if (min_idx < 0) {
            break;
        }
        nodes[min_idx].isDone = true;
        for (int i = 1; i <= m; i++) {
            if (table[min_idx][i] <= 0) {
                continue;
            }
            int cost = nodes[min_idx].cost + table[min_idx][i];
            if (nodes[i].cost < 0 || cost < nodes[i].cost) {
                nodes[i].cost = cost;
            }
        }
    }
    return nodes[end].cost;
}

int main() {
    while (true) {
        int n;
        cin >> n >> m;
        if ((n | m) == 0) {
            break;
        }
        memset(costTable, 0, sizeof(costTable));
        memset(costTable, 0, sizeof(costTable));
        for (int i = 0; i < n; i++) {
            int a, b, cost, time;
            cin >> a >> b >> cost >> time;
            costTable[a][b] = costTable[b][a] = cost;
            timeTable[a][b] = timeTable[b][a] = time;
        }
        int k;
        cin >> k;
        for (int i = 0; i < k; i++) {
            int p, q, r;
            cin >> p >> q >> r;
            cout << dijkstra(p, q, r) << endl;
        }
    }
}