#include <bits/stdc++.h>
using namespace std;

#define endl '\n'
#define pb push_back
#define pii pair<ll,ll>
typedef long long ll;

const ll logn=20,maxn=150005;
ll up[maxn][logn];
ll depth[maxn];
vector<vector<ll>> adj;

void dfs(ll v,ll p)
{
    up[v][0]=p;
    if (p==-1)depth[v]=0;
    else depth[v]=depth[p]+1;
    for (ll i=1;i<logn;i++)
    {
        if (up[v][i-1]==-1)up[v][i]=-1;
        else up[v][i]=up[up[v][i-1]][i-1];
    }
    for (auto x:adj[v])
    {
        if (x!=p)dfs(x,v);
    }
}

ll lca(ll u,ll v)
{
    if (depth[u]>depth[v])swap(u,v);
    for (int i=logn-1;i>=0;i--)
    {
        if (depth[up[v][i]]>=depth[u])v=up[v][i];
    }
    for (ll i=logn-1;i>=0;i--)
    {
        if (up[u][i]!=up[v][i])
        {
            u=up[u][i];
            v=up[v][i];
        }
    }
    return up[u][0];
}


std::vector<ll> 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
)
{
    ll n=R.size()+1;
    ll m=D.size();
    adj.resize(n);
    for (auto e:R)
    {
        adj[e.first].pb(e.second);
        adj[e.second].pb(e.first);
    }
    dfs(0,-1);
    vector<ll> ans(m);
    for (ll i=0;i<m;i++)
    {
        if (!I[i])ans[i]=0;
        else
        {
            ll w=lca(S[i],T[i]);
            ans[i]=depth[S[i]]+depth[T[i]]-2*depth[w]+1;
        }
    }
    return ans;
}
