#include <iostream>
#include <vector>
#include <cmath>
#include <queue>

#define INF (10000000)
#define MAX (100)

using namespace std;

struct edge { int to, cost; };
typedef pair<int, int> P;

vector<edge> T[MAX+1],C[MAX+1];
int dist[MAX+1];


int dijkstra_c(int s, int g)
{
    priority_queue<P, vector<P>, greater<P> > que;
    fill(dist, dist + MAX + 1, INF);
    dist[s] = 0;
    que.push(P(0, s));

    while(!que.empty()) {
        P p = que.top(); que.pop();
        int v = p.second;
        if(dist[v] < p.first) continue;
        for(int i = 0; i < C[v].size(); i++) {
            edge e = C[v][i];
            if(dist[e.to] > dist[v] + e.cost) {
                dist[e.to] = dist[v] + e.cost;
                que.push(P(dist[e.to], e.to));
            }
        }
    }
    return dist[g];
}

int dijkstra_t(int s, int g)
{
    priority_queue<P, vector<P>, greater<P> > que;
    fill(dist, dist + MAX + 1, INF);
    dist[s] = 0;
    que.push(P(0, s));

    while(!que.empty()) {
        P p = que.top(); que.pop();
        int v = p.second;
        if(dist[v] < p.first) continue;
        for(int i = 0; i < T[v].size(); i++) {
            edge e = T[v][i];
            if(dist[e.to] > dist[v] + e.cost) {
                dist[e.to] = dist[v] + e.cost;
                que.push(P(dist[e.to], e.to));
            }
        }
    }
    return dist[g];
}


int main()
{
    int n, m;
    while(cin >> n >> m && n) {
        for(int i = 0; i <= MAX; i++) {
            C[i].clear(); T[i].clear();
        }

        int a,b,c,t;
        for(int i = 0; i < n; i++) {
            cin >> a >> b >> c >> t;
            C[a].push_back((edge){b, c});
            C[b].push_back((edge){a, c});
            T[a].push_back((edge){b, t});
            T[b].push_back((edge){a, t});
        }
        
        int k,p,q,r;
        cin >> k;
        for(int i = 0; i < k; i++) {
            cin >> p >> q >> r;
            if(r == 0) cout << dijkstra_c(p, q) << endl;
            else       cout << dijkstra_t(p, q) << endl;
        }
    }
    return 0;
}