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


class Edge {
public:
    int sid, cost, time;
    Edge(int s, int c, int t) {
        sid = s;
        cost = c;
        time = t;
    }
};


class Node {
public:
    static int mode;
    int sid, cost, time;
    bool visited;
    vector<Edge*> edge;

    bool operator() (Node *a, Node *b) {
        return Node::mode == 0 ? a->cost > b->cost: a->time > b->time;
    }
};
int Node::mode = 0;


int main() {
    Node node, station[101], *s;
    priority_queue<Node*, vector<Node*>, Node> pq(node);
    int n, m, s1, s2, i, cost, time, k, p, q;

    while (cin >> n >> m && n) {
        for (i = 1; i <= m; i++) {
            station[i].sid = i;
            station[i].edge.clear();
        }
        for (i = 0; i < n; i++) {
            cin >> s1 >> s2 >> cost >> time;
            station[s1].edge.push_back(new Edge(s2, cost, time));
            station[s2].edge.push_back(new Edge(s1, cost, time));
        }

        cin >> k;
        while (k--) {
            for (i = 1; i <= m; i++) {
                station[i].cost = 3000000;
                station[i].time = 3000000;
                station[i].visited = false;
            }
            cin >> p >> q >> Node::mode;
            s = &station[p];
            s->cost = 0;
            s->time = 0;
            pq = priority_queue<Node*, vector<Node*>, Node>(node);
            pq.push(s);
            while (!pq.empty()) {
                s = pq.top(); pq.pop();
                if (s->sid == q) break;
                if (s->visited) continue;
                s->visited = true;
                for (vector<Edge*>::iterator e = s->edge.begin(); e != s->edge.end(); e++) {
                    Node *ds = &station[(*e)->sid];
                    if (Node::mode == 0) {
                        cost = s->cost + (*e)->cost;
                        if (cost < ds->cost) {
                            ds->cost = cost;
                            pq.push(ds);
                        }
                    }
                    else {
                        time = s->time + (*e)->time;
                        if (time < ds->time) {
                            ds->time = time;
                            pq.push(ds);
                        }
                    }
                }
            }
            cout << (Node::mode == 0 ? s->cost: s->time) << endl;
        }
    }

    return 0;
}