#include <cstdio>
#include <vector>
#include <queue>
using namespace std;
static const int MAX = 101;
static const int INFTY = (1 << 30);
int V;
vector< pair<int, int> > G[MAX], G2[MAX];

void dijkstra(int r, int t, int type) {
    std::priority_queue<std::pair<int, int> > pq;
    bool visited[V];
    int d[V];
    for (int i = 0; i < V; i++) {
        visited[i] = false;
        d[i] = INFTY;
    }
    d[r] = 0;
    pq.push({0, r}); //dist from target * -1, current node
    visited[r] = true;
    if (type == 0) {
        while ( !pq.empty() ) {
            pair<int, int> f = pq.top(); pq.pop();
            int current = f.second;
            visited[current] = true;

            if (d[current] < f.first * (-1)) continue;

            for (unsigned long i = 0; i < G[current].size(); i++) {
                int target = G[current][i].first;
                if (visited[target]) continue;
                if (d[target] > d[current] + G[current][i].second) {
                    d[target] = d[current] + G[current][i].second;
                    pq.push({d[target] * (-1), target});
                }
            }
        }
    }

    else if (type == 1) {
        while ( !pq.empty() ) {
            pair<int, int> f = pq.top(); pq.pop();
            int current = f.second;
            visited[current] = true;

            if (d[current] < f.first * (-1)) continue;

            for (unsigned long i = 0; i < G2[current].size(); i++) {
                int target = G2[current][i].first;
                if (visited[target]) continue;
                if (d[target] > d[current] + G2[current][i].second) {
                    d[target] = d[current] + G2[current][i].second;
                    pq.push({d[target] * (-1), target});
                }
            }
        }
    }

    printf("%d\n", d[t]);

}

int main()
{
    int E, source, target, dist, cost, type, m;
    for (; ;) {
        std::scanf("%d %d", &E, &V);
        if (E == 0 && V == 0) break;
        for (int i = 0; i < E; i++) {
            std::scanf("%d %d %d %d", &source, &target, &dist, &cost);
            source--; target--;
            G[source].push_back({target, dist});
            G[target].push_back({source, dist});
            G2[target].push_back({source, cost});
            G2[source].push_back({target, cost});
        }
        scanf("%d", &m);
        for (int i = 0; i < m; i++) {
            scanf("%d %d %d", &source, &target, &type);
            source--; target--;
            dijkstra(source, target, type);
        }
        for (int i = 0; i < V; i++) { G[i].clear(); G2[i].clear(); }
    }
    return 0;
}

