#include <bits/stdc++.h>
using namespace std;
#define file(name) if (fopen(name".INP", "r")){freopen(name".INP", "r", stdin); freopen(name".OUT", "w", stdout);}
const int maxn = 1e6 + 7;
const int LOG = log2(maxn);
int n, q, st[LOG + 1][maxn], f[LOG + 1][maxn], h[maxn];
std::vector<int> adj[maxn];
bool visited[maxn];
void dfs(){
memset(visited, false, maxn);
queue <int> q;
q.push(1);
h[1] = 1;
while (not q.empty()){
int u = q.front();
q.pop();
if (not visited[u]){
visited[u] = true;
for (auto &v: adj[u]){
if (v != st[0][u]){
st[0][v] = u;
h[v] = h[u] + 1;
for (int i = 1; i <= LOG; i++)
st[i][v] = st[i-1][st[i-1][v]];
q.push(v);
}
}
}
}
}
int lca(int u, int v){
if (h[u] < h[v]) swap(u, v);
int k = h[u] - h[v];
for (int i = LOG; i >= 0; i--)
if (k >> i & 1) u = st[i][u];
if (u == v) return u;
for (int i = LOG; i >= 0; i--)
if (st[i][u] != st[i][v]) u = st[i][u], v = st[i][v];
return st[0][u];
}
int get(int l, int r){
int k = log2(r - l + 1);
return lca(f[k][l], f[k][r - (1 << k) + 1]);
}
void solution(){
cin >> n;
for (int i = 2; i <= n; i++){
int u, v; cin >> u >> v;
adj[u].emplace_back(v);
adj[v].emplace_back(u);
}
dfs();
for (int i = 1; i <= n; i++) f[0][i] = i;
for (int i = 1; (1 << i) <= n; i++)
for (int j = 1; j <= n - (1 << i) + 1; j++)
f[i][j] = lca(f[i-1][j], f[i-1][j + (1 << (i-1))]);
cin >> q;
while (q --> 0){
int l, r; cin >> l >> r;
cout << get(l, r) << "\n";
}
}
signed main(){
ios_base::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
file("TEST");
solution();
return 0;
}
I2luY2x1ZGUgPGJpdHMvc3RkYysrLmg+CnVzaW5nIG5hbWVzcGFjZSBzdGQ7CgojZGVmaW5lIGZpbGUobmFtZSkgaWYgKGZvcGVuKG5hbWUiLklOUCIsICJyIikpe2ZyZW9wZW4obmFtZSIuSU5QIiwgInI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