#include <iostream>
#include <vector>

#define REP(i, n) for(int i = 1; i <= n; i ++)

using namespace std;

typedef pair<int, int> pii;
const int UNDEFINED = (int)1e6;
int n;

int dijkstra(int, int, vector<vector<int> >);

int main(void){
    for(int m; cin >> m >> n, n;){
        vector<vector<int> > costMap(n + 1);
        vector<vector<int> > timeMap(n + 1);

        REP(i, n){
            costMap[i].resize(n + 1);
            timeMap[i].resize(n + 1);
            REP(j, n){
                costMap[i][j] = timeMap[i][j] = UNDEFINED;
            }
        }

        while(m --){
            int a, b, cost, time;
            cin >> a >> b >> cost >> time;
            costMap[a][b] = cost;
            costMap[b][a] = cost;
            timeMap[a][b] = time;
            timeMap[b][a] = time;
        }

        int k; cin >> k;
        while(k --){
            int s, g, mode;
            cin >> s >> g >> mode;

            cout << dijkstra(s, g, ((!mode) ? costMap:timeMap)) << endl;
        }
    }

    return 0;
}

int dijkstra(int s, int g, vector<vector<int> > data){
    vector<bool> used(n + 1);
    REP(i, n) used[i] = false;

    vector<int> d(n + 1);
    REP(i, n) d[i] = (i != s) ? UNDEFINED:0;

    while(1){
        int v = UNDEFINED;
        REP(i, n) if(!used[i] && (v == UNDEFINED || d[i] < d[v])) v= i;
        if(v == UNDEFINED) break;
        used[v] = true;

        REP(i, n) d[i] = min(d[i], d[v] + data[v][i]);
    }

    return d[g];
}