#pragma GCC optimize("Ofast") #include using namespace std; typedef long long int ll; typedef unsigned long long int ull; mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count()); ll myRand(ll B) { return (ull)rng() % B; } int main() { cin.tie(nullptr); ios::sync_with_stdio(false); int n; cin >> n; vector h(n),t(n); for (int i = 0; i < n; ++i) { cin >> h[i]; } for (int i = 0; i < n; ++i) { cin >> t[i]; } vector> nx(20,vector(n)); vector mx(n); vector id(n); for (int i = 0; i < n; ++i) { id[i] = i; } sort(id.begin(), id.end(), [&](auto i,auto j){ return h[i] < h[j]; }); auto v = h; sort(v.begin(), v.end()); map mp; for (int i = 0; i < n; ++i) { mp[h[i]] = i; } int u = 0; for (int _i = 0; _i < n; ) { int i = id[_i]; int j = _i; while (_i < n and h[id[_i]] == h[i]) { u = max(u, t[id[_i]]); _i++; } auto it = lower_bound(v.begin(), v.end(), u); for (int k = j; k < _i; ++k) { if (it == v.begin()) { nx[0][id[k]] = id[k]; } else { it--; nx[0][id[k]] = mp[*it]; it++; } } } for (int i = 0; i+1 < 20; ++i) { for (int j = 0; j < n; ++j) { nx[i+1][j] = nx[i][nx[i][j]]; } } int q; cin >> q; while (q--) { int a,b; cin >> a >> b; a--; b--; if (t[a] >= h[b]) { cout << 1 << "\n"; } else { int cur = a; int res = 0; for (int k = 19; k >= 0; --k) { int nxt = cur; for (int i = k-1; i >= 0; --i) { nxt = nx[i][nxt]; if (t[nxt] >= h[b]) break; } if (t[nxt] < h[b]) { res += (1<= h[b]) { cout << res+1 << "\n"; } else { cout << -1 << "\n"; } } } }