void dfs1(int u,int f) { fa[u]=f,siz[u]=1,dep[u]=dep[f]+1; int maxson = -1; for(int i=Head[u];~i;i=Edge[i].next) { int &v = Edge[i].to; if(v==f) continue; dfs1(v,u); siz[u]+=siz[v]; if(siz[v]>maxson) maxson=siz[v],son[u]=v; } } int top[maxn]; void dfs2(int u,int t) { top[u]=t; if(!son[u]) return; dfs2(son[u],t); for(int i=Head[u];~i;i=Edge[i].next) { int &v = Edge[i].to; if(v==fa[u]||v==son[u]) continue; dfs2(v,v); } } int main() { memset(Head,-1,sizeof(Head)); int n,m,s; read(n,m,s); while(--n) { int u,v; read(u,v); AddEdge(u,v); } dfs1(s,s); dfs2(s,s); while(m--) { int u,v; read(u,v); while(top[u]!=top[v]) { if(dep[top[u]]<dep[top[v]]) std::swap(u,v); u=fa[top[u]]; } print(dep[u]<dep[v]?u:v); } }