#include <cstdio>
#include <algorithm>
#include <vector>
#include <utility>
using namespace std;

const int INF = 1<<29;

int n, m, a, b, c, t, k;
int dp[2][100][100];

int main(){
    for(; scanf("%d%d", &n, &m), n||m;){
        for(int x = 0; x < m; x++){
            fill(dp[0][x], dp[0][x]+m, INF);
            fill(dp[1][x], dp[1][x]+m, INF);
        }
        for(int x = 0; x < m; x++){
            dp[0][x][x] = dp[1][x][x] = 0;
        }
        for(int x = 0; x < n; x++){
            scanf("%d%d%d%d", &a, &b, &c, &t);
            --a; --b;
            dp[0][a][b] = c;
            dp[1][a][b] = t;
        }
        for(int z = 0; z < m; z++){
            for(int x = 0; x < m; x++){
                for(int y = 0; y < m; y++){
                    for(int w = 0; w < 2; w++){
                        dp[w][x][y] = min(dp[w][x][y],
                            dp[w][x][z] + dp[w][z][y]);
                    }
                }
            }
        }
        scanf("%d", &k);
        for(int x = 0; x < k; x++){
            scanf("%d%d%d", &a, &b, &c);
            printf("%d\n", dp[c][--a][--b]);
        }
    }
    return 0;
}