#include <bits/stdc++.h>
using namespace std;
//#include <boost/multiprecision/cpp_int.hpp>
//using multiInt = boost::multiprecision::cpp_int;

using ll = long long int;
using ld = long double;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
template<typename Q_temp>
using smaller_queue = priority_queue <Q_temp, vector<Q_temp>, greater<Q_temp> >;

const ll MOD = (ll) 998244353;
const int INF = (int) 1e9;
const ll LINF = (ll) 4e18;
const double PI = acos(-1.0);

#define REP(i,m,n) for(ll i = m; i < (ll)(n); ++i)
#define rep(i,n) REP(i, 0, n)
#define MP make_pair
#define MT make_tuple
#define YES(n) cout << ((n) ? "YES" : "NO") << endl
#define Yes(n) cout << ((n) ? "Yes" : "No") << endl
#define Possible(n) cout << ((n) ? "Possible" : "Impossible") << endl
#define all(v) v.begin(), v.end()
#define NP(v) next_permutation(all(v))
#define dbg(x_) cerr << #x_ << ":" << x_ << endl;
#define dbg2(x_) for(auto a_ : x_) cerr << a_ << " "; cerr << endl;
#define dbg3(x_ , sx_) rep(i, sx_) cerr << x_[i] << " "; cerr << endl;
vector<int> Dx = {0, 0, -1, 1, -1, 1, -1, 1, 0};
vector<int> Dy = {1, -1, 0, 0, -1, -1, 1, 1, 0};

inline ll CEIL(ll a, ll b) {
    return (a + b - 1) / b;
}

void finish() {
    cout << -1 << endl;
    exit(0);
}

//------------------------------------------------------
vector<pll> v;
vector<ll> va, vb, sva, svb;

int main() {
    cin.tie(0);
    ios::sync_with_stdio(false);
    cout << setprecision(20) << setiosflags(ios::fixed);

    int n, m;

    while (cin >> n >> m) {
        if (n == 0 && m == 0) break;

        int cost[110][110], time[110][110];

        rep(i, 110) rep(j, 110) {
            cost[i][j] = time[i][j] = (i == j ? 0 : INF);
        }

        rep(i, n) {
            int a, b, c, t;
            cin >> a >> b >> c >> t;
            cost[a][b] = cost[b][a] = c;
            time[a][b] = time[b][a] = t;
        }

        rep(k, 110) rep(i, 110) rep(j, 110) {
            cost[i][j] = min(cost[i][j], cost[i][k] + cost[k][j]);
            time[i][j] = min(time[i][j], time[i][k] + time[k][j]);
        }

        int k;
        cin >> k;

        rep(i, k) {
            int p, q, r;
            cin >> p >> q >> r;
            if (r == 0) cout << cost[p][q] << endl;
            else cout << time[p][q] << endl;
        }
    }

    return 0;
}

