#include <iostream>
#include <cstring>
#include <algorithm>
#define INF (1 << 25)
using namespace std;

int main(){
    int cost[128][128],time[128][128];
    int n,m,q,a,b,c,d,s,g,r;
    while(cin >> n >> m,n||m){
        for(int i=0;i<128;i++){
            for(int j=0;j<128;j++){
                cost[i][j] = INF; time[i][j] = INF;
            }
        }
        for(int i=0;i<n;i++){
            cin >> a >> b >> c >> d;
            cost[a][b] = c; cost[b][a] = c;
            time[a][b] = d; time[b][a] = d;
        }
        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]);
                }
            }
        }
        cin >> q;
        while(q--){
            cin >> s >> g >> r;
            cout << ((r == 0)? cost[s][g] : time[s][g]) << endl;
        }
    }
}