#include <iostream>
#include <cctype>
#include <algorithm>
#include <vector>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <utility>
#include <string>
#include <functional>
#include <cctype>
#include <cstring>
#include <cstdio>
#include <cstdlib>
#include <cmath>
typedef long long ll;
const int INF = 100000000, mod = 1000000007;
const ll LLINF = 1LL << 50;
using namespace std;

struct edge { int to, cos, tim; };
typedef pair<int, int> P;
vector<edge> G[105];
int n, m, k, cost[105], time[105];

void dijkstra(int s) {
    priority_queue<P, vector<P>, greater<P>> costque, timeque;
    fill(cost, cost + m + 1, INF);
    fill(time, time + m + 1, INF);
    cost[s] = 0;
    time[s] = 0;
    costque.push(P(s, 0));
    timeque.push(P(s, 0));

    while (!costque.empty()) {
        P p = costque.top();
        costque.pop();
        int v = p.first;
        if (cost[v] < p.second) continue;
        for (int i = 0; i < G[v].size(); i++) {
            edge e = G[v][i];
            if (cost[e.to] > cost[v] + e.cos) {
                cost[e.to] = cost[v] + e.cos;
                costque.push(P(e.to, cost[e.to]));
            }
        }
    }
    while (!timeque.empty()) {
        P p = timeque.top();
        timeque.pop();
        int v = p.first;
        if (time[v] < p.second) continue;
        for (int i = 0; i < G[v].size(); i++) {
            edge e = G[v][i];
            if (time[e.to] > time[v] + e.tim) {
                time[e.to] = time[v] + e.tim;
                timeque.push(P(e.to, time[e.to]));
            }
        }
    }
}

int main() {
    while (1) {
        cin >> n >> m;
        if (n == 0&&m == 0) break;
        for (int i = 0; i < n; i++) {
            int a, b, c, t;
            cin >> a >> b >> c >> t;
            edge tmp = { b,c,t };
            G[a].push_back(tmp);
        }
        cin >> k;
        for (int i = 0; i < k; i++) {
            int p, q, r;
            cin >> p >> q >> r;
            dijkstra(p);
            if (r == 0) cout << cost[q] << endl;
            else cout << time[q] << endl;
        }
    }
    return 0;
}