#include<bits/stdc++.h>
using namespace std;
#define ll long long
const unsigned MAXN = 15e+4 + 10;
const unsigned MAXLN = 19;

vector<vector<int> > adj(MAXN, vector<int>());
int p[MAXN];
int n, m;
int tin[MAXN], tout[MAXN], tmr=0, depth[MAXN];
void EulerTour(int u){
    tin[u] = tmr++;
    for(int k :adj[u]){
        if(k==p[u]) continue;
        p[k] = u;
        depth[k] = depth[u]+1;
        EulerTour(k);
    }
    tout[u] = tmr;
}

int bl[MAXN][MAXLN];
void initbl(){
    for(int k =0;k<=18;++k){
        for(int u =1;u<=n;++u){
            if(k)bl[u][k] = bl[bl[u][k-1]][k-1];
            bl[u][0] = p[u];
        }
    }
}

int getMSB(int k){
    int ans =0;
    for(;k>0;k>>=1)ans++;
    return ans;
}

int getAnc(int u, int k){
    while(k>0){
        int msb = getMSB(k)-1;
        k ^= (1<<msb);
        u = bl[u][msb];
    }
    return u;
}

int lca(int u, int v){
    if(depth[u] > depth[v]) swap(u,v);
    v = getAnc(v, (depth[v]-depth[u]));
    int l = 0, r = depth[u];
    int m = (l+r)>>1;
    while(l<r){
        int du = getAnc(u, m);
        int dv = getAnc(v,m);
        if(du==dv){
            r=m;
            m=(l+r>>1);
        }else{
            l = m+1;
            m =(l+r);
        }
    }
    return getAnc(u,m);
}

inline bool isAnc(int u, int v){return tin[u] <=tin[v] && tout[u] >=tout[v];}

struct balls{
    ll d;
    int s, t;
    bool i;
    void init(ll d, int s,int t, bool i){
        this->d = d;
        this->s = s;
        this->t = t;
        this->i = i;
    } 
};
inline bool compr(balls &a, balls &b){
    return a.d > b.d;
}

std::vector<int> solve(vector<pair<int, int>> R,vector<long long> D,vector<int> S,vector<int> T,vector<bool> I){
    n = (int) R.size() + 1;
    m = (int) I.size();
    for(pair<int, int>puv : R){
        int u = puv.first, v = puv.second;
        adj[u].push_back(v);
        adj[v].push_back(u);
    }
    EulerTour(0);
    initbl();
    // priority_queue<balls,vector<balls>,compr> events;
    // for(int j = 1; j<=m;++j){
    //     balls ball;
    //     ball.init(D[j], S[j], T[j], I[j]);
    //     events.push(ball);
    // }
    vector<int> ans(m);
    for(int j = 0; j<m;++j){
        int u = S[j], v= T[j];
        int lc = lca(u,v);
        ans[j] = depth[v] + depth[u] - 2*depth[lc];
    }
    return ans;
}

// int main(){
//     vector<pair<int, int> > R = {{0,1}, {1,2}, {2, 3}};
//     vector<ll> D = {1, 3};
//     vector<int> S = {1, 0}, T = {3, 2};
//     vector<bool> I={1, 0};
//     for(int a: solve(R, D, S, T, I)){
//         printf("%d ", a);
//     }
// }