#include <iostream>
#include <algorithm>
using namespace std;

int main(){
    int n, m;
    while(cin >> n >> m, n!=0 && m!=0){
        int cost[101][101];
        int time[101][101];
        for(int i=0; i<101; ++i){
            for(int j=0; j<101; ++j) time[i][j] = cost[i][j] = (i==j)?0:100000000;
        }
        for(int i=0; i<n; ++i){
            int a, b, c, t;
            cin >> a >> b >> c >> t;
            cost[a][b] = c;
            cost[b][a] = c;
            time[a][b] = t;
            time[b][a] = t;
        }
        for(int i=1; i<=m; ++i){
            for(int j=1; j<=m; ++j){
                for(int k=1; k<=m; ++k){
                    cost[j][k] = min(cost[j][k], cost[j][i]+cost[i][k]);
                    time[j][k] = min(time[j][k], time[j][i]+time[i][k]);
                }
            }
        }
        int k;
        cin >> k;
        for(int i=0; i<k; ++i){
            int p, q, r;
            cin >> p >> q >> r;
            cout << ((r==0)?cost[p][q]:time[p][q]) << endl;
        }
    }
    return 0;
}