水一水
#include<iostream>
#include<cstdio>
#define N 1000005
using namespace std;
int n,k,now,cnt;
int head[N],next[N<<1],list[N<<1];
int deep[N],fa[N][21];
inline int read()
{
int a=0,f=1; char c=getchar();
while (c<'0'||c>'9') {if (c=='-') f=-1; c=getchar();}
while (c>='0'&&c<='9') {a=a*10+c-'0'; c=getchar();}
return a*f;
}
inline void insert(int x,int y)
{
next[++cnt]=head[x];
head[x]=cnt;
list[cnt]=y;
}
void dfs(int x)
{
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 (list[i]!=fa[x][0])
{
deep[list[i]]=deep[x]+1;
fa[list[i]][0]=x;
dfs(list[i]);
}
}
inline int lca(int x,int y)
{
if (deep[x]<deep[y]) swap(x,y);
int t=deep[x]-deep[y];
for (int i=0;(1<<i)<=t;i++)
if ((1<<i)&t) x=fa[x][i];
for (int i=20;~i;i--)
if (fa[x][i]!=fa[y][i]) x=fa[x][i],y=fa[y][i];
return x==y?x:fa[x][0];
}
inline int skip(int x,int t)
{
int now=x;
for (int i=0;(1<<i)<=t;i++)
if ((1<<i)&t) now=fa[now][i];
return now;
}
int main()
{
n=read(); now=read(); k=read();
for (int i=1;i<n;i++)
{
int u=read(),v=read();
insert(u,v); insert(v,u);
}
dfs(1);
for (int i=1;i<=k;i++)
{
int next=read(),step=read(),f=lca(now,next);
int d1=deep[now]-deep[f],d2=deep[next]-deep[f];
if (step>=d1+d2) now=next;
else if (step==d1) now=f;
else if (step<d1) now=skip(now,step);
else now=skip(next,d2-(step-d1));
printf("%d ",now);
}
return 0;
}