#include <bits/stdc++.h>
using namespace std;
#define int long long
#define maxn 1000005
int n, q, s;
int tot, t;
int ver[maxn], head[maxn], Next[maxn];
int f[maxn][205], d[maxn];
void add(int x, int y) {
ver[++tot] = y;
Next[tot] = head[x], head[x] = tot;
}
void build() {
queue<int> q;
q.push(s); d[s] = 1;
while (q.size()) {
int x = q.front(); q.pop();
for (int i = head[x]; i; i = Next[i]) {
int y = ver[i];
if (d[y]) continue;
d[y] = d[x] + 1, f[y][0] = x;
for (int j = 1; j <= t; j++) {
f[y][j] = f[f[y][j - 1]][j - 1];
}
q.push(y);
}
}
}
int get_lca(int x, int y) {
if (d[x] < d[y]) swap(x, y);
for (int i = t; i >= 0; i--)
if (d[f[x][i]] >= d[y]) x = f[x][i];
if (x == y) return x;
for (int i = t; i >= 0; i--)
if (f[x][i] != f[y][i]) x = f[x][i], y = f[y][i];
return f[x][0];
}
signed main() {
cin >> n >> q >> s;
t = log(n); if (t * t != n) t++;
for (int i = 1; i <= n; i++) {
int x, y;
cin >> x >> y;
add(x, y), add(y, x);
}
build();
while (q--) {
int x, y;
cin >> x >> y;
cout << get_lca(x, y) << endl;
}
return 0;
}