#include using namespace std; #define rep(i, n) for (int i=0; i> N; vector H, T; rep(i, N) { int Hi; cin >> Hi; H.push_back(Hi); } rep(i, N) { int Ti; cin >> Ti; T.push_back(Ti); } vector> HTI; rep(i, N) HTI.push_back({H[i], T[i], i}); sort(HTI.begin(), HTI.end()); int idx[N]; rep(i, N) { H[i] = HTI[i][0]; T[i] = HTI[i][1]; idx[HTI[i][2]] = i; } int to[N+1]; to[0] = 0; rep(v, N) to[v+1] = upper_bound(H.begin(), H.end(), T[v])-H.begin(); int dp[30][N+1]; dp[0][0] = 0; for (int v=1; v<=N; v++) dp[0][v] = max(dp[0][v-1], to[v]); for (int i=1; i<30; i++) rep(v, N+1) dp[i][v] = dp[i-1][dp[i-1][v]]; int Q; cin >> Q; while (Q--) { int A, B; cin >> A >> B; A = idx[A-1]+1; B = idx[B-1]+1; int v = to[A]; int ans; if (B<=v) ans = 1; else { int ok = N+10; int ng = -1; while (abs(ok-ng)>1) { int mid = (ok+ng)/2; int now = v; rep(i, 20) if ((mid>>i)&1) now = dp[i][now]; if (B<=now) ok = mid; else ng = mid; } if (ok>N) ans = -1; else ans = ok+1; } printf("%d\n", ans); } }