package Tx;
import java.io.*;
import java.math.BigInteger;
import java.util.Arrays;
import java.util.BitSet;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.Map;
import java.util.Objects;
import java.util.PriorityQueue;
import java.util.Queue;
import java.util.Random;
import java.util.Scanner;
import java.util.Stack;
import java.util.StringTokenizer;
import java.util.Vector;
public class Tx1{
static long md=(long)998244353;
static long Linf=Long.MAX_VALUE/2;
static int inf=Integer.MAX_VALUE/2;
static
class Edge{
int fr,to,nxt;
public Edge(int u,int v) {
fr=u;
to=v;
}
}
static Edge[] e;
static int[] head;
static int cnt=0;
static void addEdge(int fr,int to) {
cnt++;
e[cnt]=new Edge(fr, to);
e[cnt].nxt=head[fr];
head[fr]=cnt;
}
static int[] fa;//直接父亲
static int[] top;//链头
static int[] size;//子树大小
static int[] dep;//深度
static int[] son;//重儿子,即在一条链上的儿子
static void dfs1(int x) {
//将树按照重儿子,分成一条条不相交的链,同时维护出深度,直接父亲,链上直接儿子
size[x]=1;
dep[x]=dep[fa[x]]+1;
for(int i=head[x];i>0;i=e[i].nxt) {
int v=e[i].to;
if(v==fa[x])continue;
fa[v]=x;
dfs1(v);
size[x]+=size[v];
if(son[x]==0||size[son[x]]<size[v]) {
son[x]=v;
}
}
}
static void dfs2(int x,int tp) {
//维护链头,当不在一条链上时,可以直接跳出
top[x]=tp;
if(son[x]!=0)dfs2(son[x], tp);
for(int i=head[x];i>0;i=e[i].nxt) {
//其他儿子与该链分开,为新链的链头
int v=e[i].to;
if(v==fa[x]||v==son[x])continue;
dfs2(v,v);
}
}
public static void main(String[] args) throws IOException{
AReader input=new AReader();
PrintWriter out = new PrintWriter(new OutputStreamWriter(System.out));
// Scanner input=new Scanner(System.in);
int n=input.nextInt();
int m=input.nextInt();
int s=input.nextInt();
e=new Edge[n*2+1];
head=new int[n+1];
cnt=0;
fa=new int[n+1];
top=new int[n+1];
size=new int[n+1];
dep=new int[n+1];
son=new int[n+1];
for(int i=1;i<n;++i) {
int u=input.nextInt();
int v=input.nextInt();
addEdge(u, v);
addEdge(v, u);
}
dfs1(s);
dfs2(s, s);
for(int i=1;i<=m;++i) {
int x=input.nextInt();
int y=input.nextInt();
while(top[x]!=top[y]) {//不在一条链上,链头深度大的通过链头往上跳
if(dep[top[x]]>dep[top[y]])x=fa[top[x]];
else y=fa[top[y]];
}
//此时在一条链上
out.println(dep[x]<dep[y]?x:y);
}
out.flush();
out.close();
}
//System.out.println();
//out.println();
static
class AReader{
BufferedReader bf;
StringTokenizer st;
BufferedWriter bw;
public AReader(){
bf=new BufferedReader(new InputStreamReader(System.in));
st=new StringTokenizer("");
bw=new BufferedWriter(new OutputStreamWriter(System.out));
}
public String nextLine() throws IOException{
return bf.readLine();
}
public String next() throws IOException{
while(!st.hasMoreTokens()){
st=new StringTokenizer(bf.readLine());
}
return st.nextToken();
}
public char nextChar() throws IOException{
//确定下一个token只有一个字符的时候再用
return next().charAt(0);
}
public int nextInt() throws IOException{
return Integer.parseInt(next());
}
public long nextLong() throws IOException{
return Long.parseLong(next());
}
public double nextDouble() throws IOException{
return Double.parseDouble(next());
}
public float nextFloat() throws IOException{
return Float.parseFloat(next());
}
public byte nextByte() throws IOException{
return Byte.parseByte(next());
}
public short nextShort() throws IOException{
return Short.parseShort(next());
}
public BigInteger nextBigInteger() throws IOException{
return new BigInteger(next());
}
public void println() throws IOException {
bw.newLine();
}
public void println(int[] arr) throws IOException{
for (int value : arr) {
bw.write(value + " ");
}
println();
}
public void println(int l, int r, int[] arr) throws IOException{
for (int i = l; i <= r; i ++) {
bw.write(arr[i] + " ");
}
println();
}
public void println(int a) throws IOException{
bw.write(String.valueOf(a));
bw.newLine();
}
public void print(int a) throws IOException{
bw.write(String.valueOf(a));
}
public void println(String a) throws IOException{
bw.write(a);
bw.newLine();
}
public void print(String a) throws IOException{
bw.write(a);
}
public void println(long a) throws IOException{
bw.write(String.valueOf(a));
bw.newLine();
}
public void print(long a) throws IOException{
bw.write(String.valueOf(a));
}
public void println(double a) throws IOException{
bw.write(String.valueOf(a));
bw.newLine();
}
public void print(double a) throws IOException{
bw.write(String.valueOf(a));
}
public void print(char a) throws IOException{
bw.write(String.valueOf(a));
}
public void println(char a) throws IOException{
bw.write(String.valueOf(a));
bw.newLine();
}
}
}
显示dfs爆栈了,想改非递归,但不会有无佬帮忙,改下dfs,求求了