#define LCA
#define CENTROID
#include "centroid.h"
#include "sgt.h"
using namespace std;
const int N = 2e5 + 1;
struct P { int i; ll d;
bool operator<(const P &o) const { return i < o.i; }
};
struct E { int u, v; ll w;
bool operator<(const E &o) const {
return v == o.v ? w > o.w : v < o.v;
}
};
vector<P> v[N];
SGT<2 * N, ll> sgt;
ll r[N];
int main() {
int n; cin >> n;
Tree<N, W, set<W, less<>>> t(n);
t.input().root(1).dfs().decompose();
for (int i = 1; i <= n; i++) {
int j = i;
while (j) {
v[j].push_back({i, t.D(i, j)});
j = t.cp[j];
}
}
vector<E> e;
for (int i = 1; i <= n; i++) {
sort(v[i].begin(), v[i].end());
vector<P> st;
for (auto [j, d] : v[i]) {
while (st.size() && st.back().d >= d) {
e.push_back({st.back().i, j, t.D(st.back().i, j)});
st.pop_back();
}
if (st.size()) e.push_back({st.back().i, j, t.D(st.back().i, j)});
st.push_back({j, d});
}
}
/* for (auto [u, v, w] : e) cout << u << " " << v << " " << w << endl; */
sgt.fn([](ll x, ll y) { return min(x, y); }, inf).fill(inf);
int q; cin >> q;
for (int i = 0; i < q; i++) {
int u, v; cin >> u >> v;
e.push_back({u, v, -i});
}
sort(e.begin(), e.end());
for (auto [u, v, w] : e) {
if (w < 1) r[-w] = sgt.query(u, n + 1);
else sgt.upd(u, w);
}
for (int i = 0; i < q; i++) {
cout << (r[i] < inf ? r[i] : -1) << endl;
}
}