#include<algorithm>
#include<climits>
#include<cmath>
#include<cstring>
#include<deque>
#include<iomanip>
#include<iostream>
#include<map>
#include<queue>
#include<set>
#include<stack>
#include<unordered_map>
#include<unordered_set>
#include<utility>
#include<vector>

using namespace std;

using lint = long long;
using P = pair<int, int>;
using LLP = pair<long long, long long>;

#define REP(i, x, n) for(int i = (x), i##_len = int(n) ; i < i##_len ; ++i)
#define rep(i, n) for(int i = 0, i##_len = int(n) ; i < i##_len ; ++i)
#define reps(i, n) for(int i = 1, i##_len = int(n) ; i <= i##_len ; ++i)
#define rrep(i, n) for(int i = int(n) - 1 ; i >= 0 ; --i)
#define rreps(i, n) for(int i = int(n) ; i > 0 ; --i)
#define SORT(x) sort((x).begin(), (x).end())
#define SORT_INV(x) sort((x).rbegin(), (x).rend())
#define TWINS(x) cout << ((x) ? "Yay!" : ":(") << endl

const int IINF = (1 << 30) - 1;
const long long LLINF = 1LL << 61;
const int dx4[] = {1, 0, -1, 0}, dy4[] = {0, 1, 0, -1};
const int dx8[] = {1, 1, 0, -1, -1, -1, 0, 1}, dy8[] = {0, -1, -1, -1, 0, 1, 1, 1};
const double EPS = 1e-8;

template<typename T>
bool chmax(T &a, T b){
    if(a < b){
        a = b;
        return true;
    }
    return false;
}

template<typename T>
bool chmin(T &a, T b){
    if(b < a){
        a = b;
        return true;
    }
    return false;
}

int main(){
    cin.tie(0);
    ios::sync_with_stdio(false);

    int n, m;
    while(cin >> n >> m, n){
        vector< vector<lint> > c(m, vector<lint>(m, LLINF)), t(m, vector<lint>(m, LLINF));
        rep(i, m){
            c[i][i] = 0;
            t[i][i] = 0;
        }

        rep(i, n){
            int a, b;
            lint co, ti;
            cin >> a >> b >> co >> ti;
            chmin(c[--a][--b], co);
            chmin(c[b][a], co);
            chmin(t[a][b], ti);
            chmin(t[b][a], ti);
        }

        rep(k, m){
            rep(i, m){
                rep(j, m){
                    chmin(c[i][j], c[i][k] + c[k][j]);
                    chmin(t[i][j], t[i][k] + t[k][j]);
                }
            }
        }

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

    return 0;
}
