#include <iostream>
#include <vector>
#include <map>
#include <set>
#include <algorithm>
#include <queue>
#include <stack>
#include <cmath>
#include <cstdio>
#include <string>
#include <sstream>

using namespace std;

#define rep(i, n) for(int i = 0; i < (int)n; ++i)
#define rep2(i, n) for(int i = 1; i <= (int)n; ++i)
#define repf(i, f, l) for(int i = f; i < l; ++i)
#define repit(it, t) for(__typeof((t).begin()) it = (t).begin(); it != (t).end(); it++)

#define pb push_back
#define lb lower_bound
#define mp make_pair
#define ite iterator
#define ul unsigned long
#define ull unsigned long long
#define INF 1000000007
#define MOD 1000000007
#define EPS 1.0e-6
#define fs first
#define sd second

template<typename T>
bool elem(set<T> st, T val){
	return (st.find(val) != st.end());
}

template<typename T>
void printv(vector<T> vt){
    if(vt.size() == 0) return;
    rep(i, vt.size() - 1)
        cout << vt[i] << " ";
    cout << vt[vt.size() - 1] << endl;
}

typedef vector<int> vint;
typedef vector<ul> vul;
typedef vector<ull> vull;
typedef pair<int, int> pii;

int edge[2][301][301];
bool vst[301];
int main(void){
    ios::sync_with_stdio(false);
    while(true){
        int n, m;
        cin >> n >> m;
        if(n == 0 && m == 0) break;
        rep(ii, 2) rep(i, m) rep(j, m) edge[ii][i][j] = -1;
        rep(i, n){
            int a, b, ed[2];
            cin >> a >> b >> ed[0] >> ed[1];
            a--; b--;
            rep(j, 2){
                edge[j][a][b] = ed[j];
                edge[j][b][a] = ed[j];
            }
        }
        int k;
        cin >> k;
        while(k-- > 0){
            rep(i, n) vst[i] = false;
            int p, q, r;
            cin >> p >> q >> r;
            p--; q--;
            set<pii> st;
            st.insert(mp(0, p));
            vst[p]++;

            int mn = 1000000009;
            while(!st.empty()){
                int  cs = (*st.begin()).fs;
                int cur = (*st.begin()).sd;
                st.erase(st.begin());
                if(cur == q){
                    mn = cs;
                    break;
                }
                vst[cur]++;
                rep(i, n)
                    if(edge[r][cur][i] > 0 && !vst[i])
                        st.insert(mp(cs + edge[r][cur][i], i));
            }
            cout << mn << "\n";
        }
    }
    return 0;
}