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

int main(void){
    while(1){
        int n,m,k;
        cin >> n >> m;
        if(n == 0 && m == 0) break;

        int costs[m+1][m+1];
        int times[m+1][m+1];
        int senro[n][4];
        for(int i=0;i<m+1;i++){
            for(int j=0;j<m+1;j++){
                costs[i][j] = 1001;
                times[i][j] = 1001;
            }
        }

        for(int i =0;i<n;i++){
            cin >> senro[i][0] >> senro[i][1] >> senro[i][2] >> senro[i][3];
        }
        for(int i = 0;i<n;i++){
            costs[senro[i][0]][senro[i][1]] = senro[i][2];
            costs[senro[i][1]][senro[i][0]] = senro[i][2];
            times[senro[i][0]][senro[i][1]] = senro[i][3];
            times[senro[i][1]][senro[i][0]] = senro[i][3];
        }
        for(int i = 1;i<m+1;i++){
            for(int k = 1;k < m+1;k++){
                for(int j = 1;j < m+1;j++){
                    costs[i][j] = min(costs[i][j],costs[i][k] + costs[k][j]);
                    times[i][j] = min(times[i][j],times[i][k] + times[k][j]);
//cout << times[i][j] << "\n";
                }
            }
        }

        cin >> k;
        int p,q,r;
        for(int i = 0;i<k;i++){
            cin >> p >> q >> r;
            if (r == 1){
                cout << times[p][q] << "\n";
            }else{
                cout << costs[p][q] << "\n";
            }

        }
    }

}

        
        