#include <vector>
#include <map>
#include <utility>

using namespace std;

int check_dist(int a, int b, vector<vector<int>> slop, int pr) {
    int dist;
    for (int i=0; i<slop[a].size(); i++) {
        if (slop[a][i] == b) {
            return 1;
        } else if (slop[a][i] == pr) {
            continue;
        } else {
            dist = check_dist(slop[a][i], b, slop, a);
            if (dist == -1) {
                continue;
            } else {
                return dist + 1;
            }
        }
    }
    return -1;
}

std::vector<int> solve(
    std::vector<std::pair<int, int>> R,
    std::vector<long long> D,
    std::vector<int> S,
    std::vector<int> T,
    std::vector<bool> I
) {

    vector<int> inf_d(S.size());
    vector<vector<int>> SLOP(R.size()+1);

    for (int i=0; i<R.size(); i++) {
        SLOP[R[i].first].push_back(R[i].second);
        SLOP[R[i].second].push_back(R[i].first);
    }

    for (int i=0; i<S.size(); i++) {
        if (I[i] == 1) {
            inf_d[i] = check_dist(S[i], T[i], SLOP, S[i]) + 1;
        } else {
            inf_d[i] = 0;
        }
    }

    return inf_d;
};
