#include <iostream>
#include <algorithm>
#include <cstring>
#include <vector>
#include <cstdio>
#define mem(a, b) memset(a, b, sizeof(a))
using namespace std;
const int maxn = 5e5 + 10;
struct node{
int v, nex;
}edge[maxn << 1];
struct Node{
int v, id;
};
int father[maxn], head[maxn], ans[maxn];
int n, m, r, cnt;
vector<Node> q[maxn];
bool vis[maxn << 1];
void init()
{
mem(head, -1);
for(int i = 0; i < maxn; i++)
father[i] = i;
}
void addedge(int u, int v)
{
edge[cnt].v = v;
edge[cnt].nex = head[u];
head[u] = cnt++;
}
int find(int x)
{
int cur = x;
while(x != father[x])
{
x = father[x];
}
while(cur != x)
{
int z = cur;
cur = father[z];
father[z] = x;
}
return x;
}
void Union(int x, int fa)
{
int fx = find(x);
int fy = find(fa);
if(fx != fy)
father[fx] = fy;
}
void tarjan(int u)
{
vis[u] = true;
for(int i = head[u]; i != -1; i = edge[i].nex)
{
int v = edge[i].v;
if(vis[v]) continue;
tarjan(v);
Union(v, u);
}
vector<Node>::iterator it = q[u].begin();
for(; it != q[u].end(); it++)
{
if(vis[(*it).v])
{
ans[(*it).id] = find((*it).v);
}
}
}
int main()
{
init();
scanf("%d%d%d", &n, &m, &r);
int x, y;
for(int i = 0; i < n - 1; i++)
{
scanf("%d%d", &x, &y);
addedge(x, y);
addedge(y, x);
}
for(int i = 0; i < m; i++)
{
scanf("%d%d", &x, &y);
Node cur;
cur.v = y;
cur.id = i;
q[x].push_back(cur);
cur.v = x;
q[y].push_back(cur);
}
tarjan(r);
for(int i = 0; i < m; i++)
printf("%d\n", ans[i]);
return 0;
}
#include <bits/stdc++.h>
using namespace std;
int n,m,s,x,y,tot=0;
const int N=500005,M=1000005;
int head[N],edge[M],Next[M];
int deep[N],fa[N][22],lg[N];
void add(int x,int y)
{
edge[++tot]=y;
Next[tot]=head[x];
head[x]=tot;
return ;
}
void dfs(int x,int y)
{
deep[x]=deep[y]+1;
fa[x][0]=y;
for(int i=1; (1<<i)<=deep[x]; i++)
fa[x][i]=fa[fa[x][i-1]][i-1];
for(int i=head[x]; i; i=Next[i])
if(edge[i]!=y)
dfs(edge[i],x);
return ;
}
int LCA(int x,int y)
{
if(deep[x]<deep[y])
swap(x,y);
while(deep[x]>deep[y])
x=fa[x][lg[deep[x]-deep[y]]-1];
if(x==y)
return x;
for(int k=lg[deep[x]]-1; k>=0; k--)
if(fa[x][k]!=fa[y][k])
{
x=fa[x][k];
y=fa[y][k];
}
return fa[x][0];
}
int main()
{
scanf("%d%d%d",&n,&m,&s);
for(int i=1; i<n; i++)
{
scanf("%d%d",&x,&y);
add(x,y);
add(y,x);
}
dfs(s,0);
for(int i=1; i<=n; i++)
lg[i]=lg[i-1]+(1<<lg[i-1]==i);
for(int i=1; i<=m; i++)
{
scanf("%d%d",&x,&y);
printf("%d\n",LCA(x,y));
}
return 0;
}