#include <bits/stdc++.h>
using namespace std;

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

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

        int E[m][m], F[m][m];
        for(int i = 0; i < m; i++){
            for(int j = 0; j < m; j++){
                E[i][j] = (i == j ? 0 : 1e9);
                F[i][j] = (i == j ? 0 : 1e9);
            }
        }

        for(int i = 0; i < n; i++){
            int a, b, c1, c2;
            cin >> a >> b >> c1 >> c2;
            a--; b--;
            E[a][b] = c1;
            E[b][a] = c1;
            F[a][b] = c2;
            F[b][a] = c2;
        }

        for(int i = 0; i < m; i++){
            for(int j = 0; j < m; j++){
                for(int k = 0; k < m; k++){
                    E[j][k] = min(E[j][k], E[j][i] + E[i][k]);
                    F[j][k] = min(F[j][k], F[j][i] + F[i][k]);
                }
            }
        }

        int k;
        cin >> k;
        for(int i = 0; i < k; i++){
            int p, q, r;
            cin >> p >> q >> r;
            p--; q--;
            if(r == 0){
                cout << E[p][q] << endl;
            }else{
                cout << F[p][q] << endl;
            }
        }
    }

    return 0;
}
