#include <cstdio>
#include <vector>
#include <queue>
#include <algorithm>

#define MAX_M 100
#define INF 1145141919

class edge{
public:
    int to;
    int cost;
    int time;
};

typedef std::pair<int, int> P;

int n, m, a, b, c, t, k, s, g, r;
int value[2][MAX_M + 1];
std::vector<edge> G[MAX_M + 1];


int ret(edge e){
    if(r == 0) return e.cost;
    return e.time;
}

void dij(){
    std::fill(value[r], value[r] + MAX_M + 1, INF);
    value[r][s] = 0;

    std::priority_queue<P, std::vector<P>, std::greater<P> > que;
    que.push(P(0, s));

    while(!que.empty()){
        P p = que.top(); que.pop();
        if(value[r][p.second] < p.first) continue;

        for(int i = 0; i < G[p.second].size(); i++){
            edge e = G[p.second][i];
            int u = ret(e);
            if(value[r][e.to] > value[r][p.second] + u){
                value[r][e.to] = value[r][p.second] + u;
                que.push(P(value[r][e.to], e.to));
            }
        }
    }

    printf("%d\n", value[r][g]);
}

int main(){

    while(scanf("%d %d", &n, &m) != EOF){
        if(n == 0 && m == 0) break;

        for(int i = 0; i < n; i++){
            scanf("%d %d %d %d", &a, &b, &c, &t);
            a--; b--;


            edge e1, e2;
            e1.to   = b; e2.to = a;
            e1.cost = c; e2.cost = c;
            e1.time = t; e2.time = t;
            G[a].push_back(e1);
            G[b].push_back(e2);
        }


        scanf("%d", &k);
        for(int i = 0; i < k; i++){
            scanf("%d %d %d", &s, &g, &r);
            s--; g--;

            dij();
        }
    }

    return 0;
}