#include <iostream>
using namespace std;

#define rep(i, n) for(int i = 0;i < (int)n;i++)

int c[100][100];
int t[100][100];

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

        rep(i, m) {
            rep(j, m) {
                c[i][j] = t[i][j] = (i != j ? 1001 : 0);
            }
        }

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

        rep(i, m) {
            rep(j, m) {
                rep(k, m) {
                    c[i][j] = min(c[i][j], c[i][k] + c[k][j]);
                    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;
            p--; q--;
            cout << (r == 0 ? c[p][q] : t[p][q]) << endl;
        }
    }

    return 0;
}