#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using P = pair<int, int>;

const ll INF = 1001001001001;

ll ti[111][111];
ll co[111][111];

int main() {
    ll n, m;
    while (cin >> n >> m && !(n == 0 && m == 0)) {
        for (int i = 0; i < 111; i++) {
            for (int j = 0; j < 111; j++) {
                ti[i][j] = co[i][j] = INF;
            }
        }
        ll a, b, c, t;
        for (int i = 0; i < n; i++) {
            cin >> a >> b >> c >> t;
            a--; b--;
            co[a][b] = co[b][a] = c;
            ti[a][b] = ti[b][a] = t;
        }
        for (int k = 0; k < m; k++) {
            for (int i = 0; i < m; i++) {
                for (int j = 0; j < m; j++) {
                    ti[i][j] = min(ti[i][j], ti[i][k] + ti[k][j]);
                    co[i][j] = min(co[i][j], co[i][k] + co[k][j]);
                }
            }
        }
        int k;
        cin >> k;
        for (int i = 0, p, q, r; i < k; i++) {
            cin >> p >> q >> r;
            p--; q--;
            if (r == 0) cout << co[p][q] << endl;
            else cout << ti[p][q] << endl;
        }
    }
}
