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

int main() {
    while (true) {
        int n, m;
        cin >> n >> m;
        
        if (n==0 && m==0) {
            break;
        }
        
        int d_cost[m][m];
        int d_time[m][m];
        
        for (int i=0; i<m; i++) {
            for (int j=0; j<m; j++) {
                d_cost[i][j] = INF;
                d_time[i][j] = INF;
            }
        }
        
        for (int i=0; i<n; i++) {
            int ai, bi, cost_i, time_i;
            cin >> ai >> bi >> cost_i >> time_i;
            d_cost[ai-1][bi-1] = cost_i;
            d_cost[bi-1][ai-1] = cost_i;
            d_time[ai-1][bi-1] = time_i;
            d_time[bi-1][ai-1] = time_i;
        }
        
        for (int k=0; k<m; k++) {
            for (int i=0; i<m; i++) {
                for (int j=0; j<m; j++) {
                    d_cost[i][j] = min(d_cost[i][j], d_cost[i][k]+d_cost[k][j]);
                    d_time[i][j] = min(d_time[i][j], d_time[i][k]+d_time[k][j]);
                }
            }
        }
        
        int k;
        cin >> k;
        
        for (int i=0; i<k; i++) {
            int pi, qi, ri;
            cin >> pi >> qi >> ri;
            
            if (ri==0) {
                cout << d_cost[pi-1][qi-1] << endl;
            }
            else {
                cout << d_time[pi-1][qi-1] << endl;
            }
        }
    }
}
