#include <bits/stdc++.h>
#define rep(i, n) for(int i = 0; i < (int)(n); i++)
#define pb push_back
#define mp make_pair

using namespace std;

typedef long long ll;
typedef pair<int, int> P;

const int MOD = 1000000007;
//const int INF = 2000000000;
const int INF = (1 << 21);

int c[1000][1000];
int t[1000][1000];

int main() {
    while(true) {
        int n, m;
        cin >> n >> m;
        if(n == 0 && m == 0) break;

        rep(i, m) {
            rep(j, m) {
                c[i][j] = INF;
                t[i][j] = INF;
            }
            c[i][i] = 0;
            t[i][i] = 0;
        }
        rep(i, n) {
            int a, b, cost, time;
            cin >> a >> b >> cost >> time;
            c[a - 1][b - 1] = cost;
            c[b - 1][a - 1] = cost;
            t[a - 1][b - 1] = time;
            t[b - 1][a - 1] = time;
        }

        rep(k, m) {
            rep(i, m) {
                rep(j, m) {
                    /*if(c[i][k] != INF && c[k][j] != INF)*/ c[i][j] = min(c[i][j], c[i][k] + c[k][j]);
                    /*if(t[i][k] != INF && t[k][j] != INF)*/ t[i][j] = min(t[i][j], t[i][k] + t[k][j]);
                }
            }
        }

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

    return 0;
}

