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

#define INF 100000000

using namespace std;

int main()
{
    int n, m;
    while(cin >> n >> m && n) {
        if(n == 0 || m == 0) break;
        int times[m+1][m+1], costs[m+1][m+1];
        int a,b,cost,time;
        for(int i = 1; i <= m; i++) 
            for(int j = 1; j <= m; j++) 
                times[i][j] = costs[i][j] = (i==j) ? 0 : INF; 
        
        for(int i = 0; i < n; i++) {
            cin >> a >> b >> cost >> time;
            costs[a][b] = costs[b][a] = cost;
            times[a][b] = times[b][a] = time;
        }

        for(int a = 1; a <= m; a++) {
            for(int b = 1; b <= m; b++) {
                for(int c = 1; c <= m; c++) {
                    times[a][b] = min(times[a][b], times[a][c] + times[c][b]);
                    costs[a][b] = min(costs[a][b], costs[a][c] + costs[c][b]);
                }
            }
        }

        int p,q,r,qn;
        cin >> qn;
        for(int i = 0; i < qn; i++) {
            cin >> p >> q >> r;
            if(r == 1) cout << times[p][q] << endl;
            else       cout << costs[p][q] << endl;
        }
    }
    return 0;
}