/*
* C. Terror in Terminus | SRBD Code Contest - 2024 (Round 1)
* Author: Sohom Sahaun | @sohomsahaun | CF: sahaun
*/
#include <bits/stdc++.h>
using namespace std;
#define FAST ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL)
using ll = long long;
struct BinaryLifting {
int n;
vector<int> depth, par, jump;
BinaryLifting() {}
BinaryLifting(int n, int root = 0) : n(n) {
depth = vector<int>(n, 0);
par = vector<int>(n, -1);
jump = vector<int>(n, root);
}
void set_root(int u, int root) {
jump[u] = root;
}
void add_child(int u, int v) {
depth[v] = depth[u]+1;
par[v] = u;
jump[v] = u;
int x = jump[u];
if (depth[u]-depth[x] == depth[x]-depth[jump[x]]) {
jump[v] = jump[x];
}
}
int get_parent(int u) {
return par[u];
}
int get_depth(int u) {
return depth[u];
}
int get_ancestor_at_depth(int u, int d) {
if (d < 0) return -1;
while (depth[u] > d) {
u = (depth[jump[u]] < d) ? par[u] : jump[u];
}
return u;
}
int get_kth_ancestor(int u, int k) {
return get_ancestor_at_depth(u, depth[u]-k);
}
int get_lca(int u, int v) {
if (depth[u] > depth[v]) swap(u, v);
v = get_ancestor_at_depth(v, depth[u]);
while (u != v) {
if (jump[u] == jump[v]) {
u = par[u];
v = par[v];
} else {
u = jump[u];
v = jump[v];
}
}
return u;
}
int get_path_length(int u, int v) {
int x = get_lca(u, v);
return (depth[u]-depth[x] + depth[v]-depth[x]);
}
bool is_in_path(int z, int u, int v) {
int x = get_lca(u, v);
if (get_lca(x, z) == x) {
if (get_lca(u, z) == z) return true;
if (get_lca(v, z) == z) return true;
}
return false;
}
};
const int MX = 100005;
vector<int> g[MX];
BinaryLifting lift;
void lca_dfs(int u, int p = -1) {
for (int v : g[u]) {
if (v == p) continue;
lift.add_child(u, v);
lca_dfs(v, u);
}
}
void init(int n) {
for (int i = 0; i <= n; ++i) {
g[i].clear();
}
lift = BinaryLifting(n);
}
int main() {
FAST;
int tc = 1, ti;
cin >> tc;
for (ti = 1; ti <= tc; ++ti) {
int n, q, i, u, v, x, uv, ux, vx, req, z;
cin >> n;
init(n);
for (i = 1; i < n; ++i) {
cin >> u >> v;
--u; --v;
g[u].push_back(v);
g[v].push_back(u);
}
lca_dfs(0);
cin >> q; while (q--) {
cin >> u >> v;
--u; --v;
x = lift.get_lca(u, v);
uv = lift.get_path_length(u, v);
ux = lift.get_path_length(u, x);
vx = lift.get_path_length(v, x);
req = uv/2;
z = -1;
if (ux >= req) {
z = lift.get_kth_ancestor(u, req);
} else {
z = lift.get_kth_ancestor(v, vx - (req - ux));
}
cout << z+1 << "\n";
}
}
return 0;
}
LyoKICogIEMuIFRlcnJvciBpbiBUZXJtaW51cyB8IFNSQkQgQ29kZSBDb250ZXN0IC0gMjAyNCAoUm91bmQgMSkKICogIEF1dGhvcjogU29ob20gU2FoYXVuIHwgQHNvaG9tc2FoYXVuIHwgQ0Y6IHNhaGF1bgogKi8KIAojaW5jbHVkZSA8Yml0cy9zdGRjKysuaD4KdXNpbmcgbmFtZXNwYWNlIHN0ZDsKCiNkZWZpbmUgRkFTVCBpb3NfYmFzZTo6c3luY193aXRoX3N0ZGlvKGZhbHNlKTtjaW4udGllKE5VTEwpO2NvdXQudGllKE5VTEwpCnVzaW5nIGxsID0gbG9uZyBsb25nOwoKc3RydWN0IEJpbmFyeUxpZnRpbmcgewogIGludCBuOwogIHZlY3RvcjxpbnQ+IGRlcHRoLCBwYXIsIGp1bXA7CgogIEJpbmFyeUxpZnRpbmcoKSB7fQogIEJpbmFyeUxpZnRpbmcoaW50IG4sIGludCByb290ID0gMCkgOiBuKG4pIHsKICAgIGRlcHRoID0gdmVjdG9yPGludD4obiwgMCk7CiAgICBwYXIgPSB2ZWN0b3I8aW50PihuLCAtMSk7CiAgICBqdW1wID0gdmVjdG9yPGludD4obiwgcm9vdCk7CiAgfQoKICB2b2lkIHNldF9yb290KGludCB1LCBpbnQgcm9vdCkgewogICAganVtcFt1XSA9IHJvb3Q7CiAgfQoKICB2b2lkIGFkZF9jaGlsZChpbnQg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