1 #include<iostream> 2 #include<cstdio> 3 #include<cstring> 4 #include<cstdio> 5 #include<algorithm> 6 using namespace std; 7 #define maxn 1000010 8 #define IL inline 9 IL void read(int &x) 10 { 11 x=0;int f=1;char ch=getchar(); 12 while(ch<'0'||ch>'9'){if(ch=='-')f=-f;ch=getchar();} 13 while(ch>='0'&&ch<='9'){x=x*10+ch-'0',ch=getchar();}x*=f; 14 } 15 16 int n,m,s,tot; 17 int first[maxn],deep[maxn],fa[maxn],son[maxn],size[maxn],top[maxn]; 18 struct edge{ 19 int nextx,to; 20 }e[maxn]; 21 22 void add(int u,int v) 23 { 24 tot++; 25 e[tot].nextx=first[u]; 26 first[u]=tot; 27 e[tot].to=v; 28 } 29 30 void dfs1(int x,int f,int depth) 31 { 32 fa[x]=f; 33 deep[x]=depth; 34 size[x]=1; 35 int mson=-1; 36 for(int i=first[x];i;i=e[i].nextx) 37 { 38 int u=e[i].to; 39 if(u==f)continue; 40 dfs1(u,x,depth+1); 41 size[x]+=size[u]; 42 if(size[u]>mson) 43 mson=size[u],son[x]=u; 44 } 45 } 46 47 void dfs2(int x,int topf) 48 { 49 top[x]=topf; 50 if(!son[x])return; 51 dfs2(son[x],topf); 52 for(int i=first[x];i;i=e[i].nextx) 53 { 54 int u=e[i].to; 55 if(u==fa[x]||u==son[x])continue; 56 dfs2(u,u); 57 } 58 } 59 60 int main() 61 { 62 read(n),read(m),read(s); 63 for(int i=1;i<=n-1;i++) 64 { 65 int a,b; 66 read(a),read(b); 67 add(a,b),add(b,a); 68 } 69 dfs1(s,s,1); 70 dfs2(s,s); 71 for(int i=1;i<=m;i++) 72 { 73 int x,y; 74 read(x),read(y); 75 while(top[x]!=top[y]) 76 { 77 if(deep[top[x]]>=deep[top[y]])x=fa[top[x]]; 78 else y=fa[top[y]]; 79 } 80 if(deep[x]<=deep[y])cout<<x<<" "; 81 else cout<<y<<" "; 82 } 83 return 0; 84 }