#include <bits/stdc++.h>
#define pii pair<int,int>
#define pb push_back
#define ll long long
using namespace std;

const int nmax=150000;

int h[nmax+1],jump[nmax+1],parent[nmax+1],tin[nmax+1],tout[nmax+1],timer;

bool infected[nmax+1];

vector<int>v[nmax+1];

void dfs(int nod,int pr){
    ++timer;
    tin[nod]=timer;
    h[nod]=h[pr]+1;
    parent[nod]=pr;
    int u1=jump[pr],u2=jump[u1];
    if(h[u2]-h[u1]==h[u1]-h[pr])
        jump[nod]=u2;
    else jump[nod]=pr;
    for(int i:v[nod])
        if(!h[i])
            dfs(i,nod);
    ++timer;
    tout[nod]=timer;
}

bool ancestor(int a,int b){///a ancestor of b
    return tin[a]<=tin[b] && tout[b]<=tout[a];
}


int lca(int a,int b){
    while(!ancestor(a,b)){
        int ja=jump[a];
        if(!ancestor(ja,b))
            a=jump[a];
        else a=parent[a];
    }
    return a;
}

int dist(int a,int b){
    int l=lca(a,b);
    return h[a]+h[b]-h[l];
}

vector<int> solve(vector<std::pair<int, int>> R,vector<long long> D,vector<int> S,vector<int> T,vector<bool> I) {
    timer=0;
    int n=R.size()+1;
    int m=I.size();
    for(int i=0;i<m;i++)
        infected[i]=I[i];

    for(auto [st,dr]:R){
        v[st+1].pb(dr+1);
        v[dr+1].pb(st+1);
    }
    h[1]=0;
    dfs(1,1);
    vector<int>answer;



    for(int i=0;i<m;i++)
        answer.pb(dist(S[i]+1,T[i]+1))*infected[i];
    return answer;

}