#include <iostream>
#define INFINITY 1000000
int main (){
    int m,n;
    std::cin >> n >> m;
    int table_c[100][100];
    int table_j[100][100];
    for (size_t i = 1; i <= m; i++)
    {
        for (size_t j = 1; j <= m; j++)
        {
            if(i==j){
                table_c[i][j] = 0;
                table_j[i][j] = 0;
            }else{
                table_c[i][j] = INFINITY;
                table_j[i][j] = INFINITY;
            }
            
        }
        
    }
    
    for (size_t i = 1; i <= n; i++)
    {
        int a,b,cost,jikan;
        std::cin >> a >> b >> cost >> jikan;
        table_c[a][b] = cost;
        table_c[b][a] = cost;
        table_j[a][b] = jikan;
        table_j[b][a] = jikan;
    }

    for (size_t i = 1; i <= m; i++)
    {
        for (size_t j = 1; j <= m; j++)
        {   
            for (size_t k = 1; k <= m; k++)
            {
                table_c[i][j] = std::min(table_c[i][k] + table_c[k][j], table_c[i][j]);
                table_j[i][j] = std::min(table_j[i][k] + table_j[k][j], table_j[i][j]);

            }
        }
    }
    
    int k;
    std::cin >> k;
    int p[50],q[50],r[50];
    for (size_t i = 0; i < k; i++)std::cin >> p[i] >> q[i] >> r[i];
    for (size_t i = 0; i < k; i++){
        if (r[i]){
            std::cout << table_j[p[i]][q[i]] << std::endl;

        }else{
            std::cout << table_c[p[i]][q[i]] << std::endl;
        }
    }
}
