RT.
#include <iostream>
#include <cstdio>
#pragma GCC optmize(2)
using namespace std;
struct edge
{
int u, v, nxt;
} a[1000010];
int f[500010][23], dep[500010], edgegs, head[500010];
int addln(int u, int v)
{
a[++edgegs].u = u;
a[edgegs].v = v;
a[edgegs].nxt = head[u];
head[u] = edgegs;
}
int log_2[1000010];
int rc(int fa, int now)
{
f[now][0] = fa;
dep[now] = dep[fa] + 1;
for (int i = 1; i <= log_2[dep[now]]; i++)
f[now][i] = f[f[now][i - 1]][i - 1];
for (int i = head[now]; i; i = a[i].nxt)
if (a[i].v != fa)
rc(now, a[i].v);
}
int lca(int l, int r)
{
if (dep[r] < dep[l])
swap(l, r);
while (dep[r] > dep[l])
r = f[r][log_2[dep[r] - dep[l]] - 1];
if (l == r)
return l;
for (int i = log_2[dep[l]] - 1; i >= 0; i--)
{
if (f[l][i] != f[r][i])
{
l = f[l][i];
r = f[r][i];
}
}
return f[r][0];
}
int main()
{
int n, m, s;
scanf("%d%d%d", &n, &m, &s);
int u, v;
for (int i = 1; i < n; i++)
log_2[i] = log_2[i - 1] + (1 << log_2[i - 1] == i);
for (int i = 1; i < n; i++)
{
scanf("%d%d", &u, &v);
addln(u, v);
addln(v, u);
}
rc(0, s);
for (int i = 0; i < m; i++)
{
scanf("%d%d", &u, &v);
printf("%d\n", lca(u, v));
}
return 0;
}