#include<iostream>
#include<map>
#include<queue>
using namespace std;

#define P pair<int, int>
#define MAX_M 300
#define MAX_N 100

int n, m, k, trainMap[MAX_N + 1][MAX_N + 1][2];

void resetTrainMap();
void inputTrainMap(int n);
int check(int p, int q, int r);

int main(){
    while(cin >>n >>m){
        if(n == 0 && m == 0) break;
        resetTrainMap();
        inputTrainMap(n);
        cin >>k;
        for(int i = 0; i < k; i++){
            int p, q, r;
            cin >>p >>q >>r;
            cout <<check(p, q, r) <<endl;
        }
    }
    return 0;
}

void resetTrainMap(){
    for(int i = 0; i <= MAX_N; i++){
        for(int j = 0; j <= MAX_N; j++){
            trainMap[i][j][0] = -1;
            trainMap[i][j][1] = -1;
        }
    }
}

void inputTrainMap(int n){
    for(int i = 0; i < n; i++){
        int a, b, c, t;
        cin >>a >>b >>c >>t;
        trainMap[a][b][0] = c;
        trainMap[b][a][0] = c;
        trainMap[a][b][1] = t;
        trainMap[b][a][1] = t;
    }
}

int check(int p, int q, int r){
    priority_queue< P, vector<P>, greater<P> > ansQu; //f = cost or time
    map<int, int> ansMap;
    for(int i = 1; i <= m; i++) ansMap.insert( map<int, int>::value_type(i, -1) );
    ansQu.push( make_pair(p, 0) );
    while(!ansQu.empty()){
        P nowq = ansQu.top();
        for(int i = 1; i <= m; i++){
            if(trainMap[nowq.first][i][r] != -1){
                if(nowq.second + trainMap[nowq.first][i][r] < ansMap[i] || ansMap[i] == -1){
                    ansMap[i] = nowq.second + trainMap[nowq.first][i][r];
                    ansQu.push( make_pair(i, ansMap[i]) );
                    //                    cout <<"push! i = " <<i <<", cost or time = " <<ansMap[i] <<endl;
                }
            }
        }
        ansQu.pop();
    }
    return ansMap[q];
}