#include <iostream> 
#include<vector>
#include<algorithm>
#include<map>
#include<iomanip>
#include<set>
#include<queue>
#include<deque>
#include<iomanip>
#include<sstream>
#include<cmath>
#include<bitset>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define req(i,n) for(ll i = 1;i <=  n; i++)
#define rreq(i,n) for(ll i = n;i >= 1;i--)
#define ALL(obj) begin(obj), end(obj)
#define RALL(a) rbegin(a),rend(a)
typedef long long int ll;
typedef long double ld;
template<typename A, size_t N, typename T>
void Fill(A(&array)[N], const T & val) {
    std::fill((T*)array, (T*)(array + N), val);
}
const int MOD = 1000000007;
int main(void) {
    int n, m;
    while (cin >> n >> m, n, m) {
        vector<vector<int>> dp1(m, vector<int>(m,MOD)), dp2(m, vector<int>(m, MOD));
        rep(i, n) {
            int a, b, c, t; cin >> a >> b >> c >> t; a--; b--;
            dp1[a][b] = c; dp2[a][b] = t;
            dp1[b][a] = c; dp2[b][a] = t;
        }rep(i, m) rep(j, m) rep(k, m) dp1[j][k] = min(dp1[j][k], dp1[j][i] + dp1[i][k]);
        rep(i, m) rep(j, m) rep(k, m) dp2[j][k] = min(dp2[j][k], dp2[j][i] + dp2[i][k]);
        int k; cin >> k;
        rep(i, k) {
            int a, b, r; cin >> a >> b >> r; a--; b--;
            if (r == 0) {
                cout << dp1[a][b] << endl;
            }
            else {
                cout << dp2[a][b] << endl;
            }
        }
    }
}
