#include namespace { #pragma GCC diagnostic ignored "-Wunused-function" #include #pragma GCC diagnostic warning "-Wunused-function" using namespace std; using namespace atcoder; #define rep(i,n) for(int i = 0; i < (int)(n); i++) #define rrep(i,n) for(int i = (int)(n) - 1; i >= 0; i--) #define all(x) begin(x), end(x) #define rall(x) rbegin(x), rend(x) template bool chmax(T& a, const T& b) { if (a < b) { a = b; return true; } else return false; } template bool chmin(T& a, const T& b) { if (b < a) { a = b; return true; } else return false; } using ll = long long; using P = pair; using VI = vector; using VVI = vector; using VL = vector; using VVL = vector; } int main() { ios::sync_with_stdio(false); cin.tie(0); int n; cin >> n; VI h(n), t(n); rep(i, n) cin >> h[i]; rep(i, n) cin >> t[i]; vector ord(n); iota(ord.begin(), ord.end(), 0); sort(ord.begin(), ord.end(), [&](int i, int j) { return h[i] < h[j]; }); VI rank(n); rep(i, n) rank[ord[i]] = i; VI to(n, -1); VI to_d[18]; rep(u, n) { auto it = upper_bound(all(ord), t[u], [&](int x, int i) { return x < h[i]; }); if (it != ord.begin()) to[u] = *prev(it); } to_d[0].resize(n); { int now = ord[0]; rep(i, n) { int u = ord[i]; if (rank[u] > rank[now]) now = u; if (to[u] != -1 && rank[to[u]] > rank[now]) now = to[u]; to_d[0][u] = now; } } rep(i, 17) { to_d[i+1].resize(n, -1); rep(v, n) to_d[i+1][v] = to_d[i][to_d[i][v]]; } int q; cin >> q; while(q--) { int a, b; cin >> a >> b; a--, b--; if (to[a] == -1) { cout << -1 << '\n'; continue; } int ans = 1; a = to[a]; rrep(k, 18) { int v = to_d[k][a]; if (rank[v] < rank[b]) ans += 1 << k, a = v; } if (rank[a] < rank[b]) ans++, a = to_d[0][a]; if (rank[a] < rank[b]) ans = -1; cout << ans << '\n'; } }