#include <iostream>
#include <cstdio>
#include <string>
#define MN 3000050
using namespace std;
int dfn[MN], f[MN][21];
int n, m, cnt, root;
int head[MN], lg[MN];
int a[MN];
int sum[MN];
struct tu {
int v, nxt;
} e[MN];
void add(int u, int v) {
e[++cnt].v = v;
e[cnt].nxt = head[u];
head[u] = cnt;
}
void dfs(int now, int fa) {
dfn[now] = dfn[fa] + 1;
f[now][0] = fa;
for (int i = 1; (1 << i) <= dfn[now]; i++) f[now][i] = f[f[now][i - 1]][i - 1];
for (int i = head[now]; i; i = e[i].nxt) {
if (e[i].v != fa)
dfs(e[i].v, now);
}
}
int lca(int x, int y) {
if (dfn[x] < dfn[y])
swap(x, y