#include <bits/stdc++.h>

using namespace std;
using ll = long long;

const int N = 200009, L = 18;

ll d[N];
bool e[N];
vector < int > g[N];
int n, m, tme, tin[N], tout[N], fr[N], to[N], dep[N], up[N][L];

void pre(int v, int p, int len) {
    up[v][0] = p;
    tin[v] = ++tme;
    for (int i = 1; i < L; i++) up[v][i] = up[up[v][i - 1]][i - 1];
    dep[v] = len;
    for (auto u : g[v]) if (u != p) pre(u, v, len + 1);
    tout[v] = tme;
}

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

int lca(int u, int v) {
    if (upper(u, v)) return u;
    for (int i = L - 1; i >= 0; i--) if (!upper(up[u][i], v)) u = up[u][i];
    return up[u][0];
}

vector < int > solve(vector<pair<int,int>> ed, vector<ll> dd, vector<int> ss, vector<int> tt, vector<bool> ee) {
    m = (int)dd.size();
    n = (int)ed.size() + 1;

    for (int i = 1; i <= m; i++) {
        fr[i] = ss[i - 1] + 1, to[i] = tt[i - 1] + 1;
        e[i] = ee[i - 1], d[i] = dd[i - 1];
    }

    for (int i = 0; i < n - 1; i++) {
        int u = ed[i].first + 1, v = ed[i].second + 1;
        g[u].push_back(v);
        g[v].push_back(u);
    }

    pre(1, 1, 0);

    vector < int > res(m);

    for (int i = 1; i <= m; i++) {
        if (!e[i]) res[i - 1] = 0;
        else {
            int p = lca(fr[i], to[i]);
            res[i - 1] = dep[fr[i]] + dep[to[i]] - 2 * dep[p] + 1;
        }
    }

    return res;
}