#include<bits/stdc++.h> #define int long long #define il inline usingnamespace std;
il intread() { int x = 0; char c = getchar(); while (c < '0') c = getchar(); while (c >= '0') { x = x * 10 + (c - '0'); c = getchar(); } return x; }
int n, m, s; vector<int> edge[500005]; int lca[500005][25], dep[500005]; voiddfs(int x, int fa) { lca[x][0] = fa; dep[x] = dep[fa] + 1; int now = edge[x].size(); for (int i = 0; i < now; i++) { if (edge[x][i] == fa) continue; dfs(edge[x][i], x); } return ; } voidpre() { for (int j = 1; j <= 20; j++) { for (int i = 1; i <= n; i++) lca[i][j] = lca[lca[i][j - 1]][j - 1]; } return ; } il intLCA(int x, int y) { if (dep[x] < dep[y]) swap(x, y); for (int i = 20; i >= 0; i--) { if (dep[lca[x][i]] >= dep[y]) x = lca[x][i]; } if (x == y) return x; for (int i = 20; i >= 0; i--) { if (lca[x][i] != lca[y][i]) x = lca[x][i], y = lca[y][i]; } return lca[x][0]; } signedmain() { n = read(); m = read(); s = read(); for (int i = 1; i < n; i++) { int x, y; x = read(); y = read(); edge[x].push_back(y); edge[y].push_back(x); } dfs(s, 0); pre(); for (int i = 1; i <= m; i++) { int x, y; x = read(); y = read(); cout << LCA(x, y) << "\n"; } return0; }