#include <iostream>
using namespace std;
int main(void) {
    // Your code here!
    int INF = 10000000;
    
    while(true) {
        int n, m;
        cin >> n >> m;
        if(n == 0 && m == 0) break;
        
        int tableC[m][m];
        int tableT[m][m];
        
        for(int i = 0; i < m; i++) {
            for(int j = 0; j < m; j++) {
                tableC[i][j] = INF;
                tableT[j][i] = INF;
            }
        }
        
        int a, b, c, t;
        for(int k = 0; k < n; k++) {
            cin >> a >> b >> c >> t;
            tableC[a-1][b-1] = c;           
            tableC[b-1][a-1] = c;            
            tableT[a-1][b-1] = t;            
            tableT[b-1][a-1] = t;            
        }
        
        for(int k = 0; k < m; k++) {
            for(int i = 0; i < m; i++) {
                for(int j = 0; j < m; j++) {
                    if(tableT[i][j] > tableT[i][k] + tableT[k][j]) {
                        tableT[i][j] = tableT[i][k] + tableT[k][j];
                    }
                    if(tableC[i][j] > tableC[i][k] + tableC[k][j]) {
                        tableC[i][j] = tableC[i][k] + tableC[k][j];
                    }
                }
            }
        }
        
        int l, p, q, r;
        cin >> l;
        for(int i = 0; i < l; i++) {
            cin >> p >> q >> r;
            if(r == 0) {
                cout << tableC[p-1][q-1] << endl;
            } else {
                cout << tableT[p-1][q-1] << endl;
            }
        }
    }
}

