#include <iostream>
#include <algorithm>
using namespace std;


int d[1000][1000][2];
int main(){
    int n, m;
    while (cin >> n >> m && n | m){
        for (int i = 0; i < n; i++){
            for (int j = 0; j < n; j++){
                d[i][j][0] = d[i][j][1] = 1 << 29;
                if (i == j)d[i][j][0] = d[i][j][1] = 0;
            }
        }
        for (int i = 0; i < n; i++){
            int a, b, c, dd;
            cin >> a >> b >> c >> dd;
            a--, b--;
            d[a][b][0] = d[b][a][0] = c;
            d[a][b][1] = d[b][a][1] = dd;
        }
        for (int k = 0; k < n; k++){
            for (int i = 0; i < n; i++){
                for (int j = 0; j < n; j++){
                    d[i][j][0] = min(d[i][j][0], d[i][k][0] + d[k][j][0]);
                    d[i][j][1] = min(d[i][j][1], d[i][k][1] + d[k][j][1]);
                }
            }
        }
        int q; cin >> q;
        while (q--){
            int x, y; cin >> x >> y;
            x--, y--;
            int a; cin >> a;
            cout << d[x][y][a] << endl;
        }
    }
}