#include #include #include #include #include using namespace std; using i32 = int; using u32 = unsigned int; using i64 = long long; using u64 = unsigned long long; #define rep(i,n) for(int i=0; i<(int)(n); i++) const i64 INF = 1001001001001001001; using Modint = atcoder::static_modint<998244353>; int main(){ int N; cin >> N; vector H(N), T(N); rep(i,N) cin >> H[i]; rep(i,N) cin >> T[i]; vector TI(N); rep(i,N) TI[i] = i; sort(TI.begin(), TI.end(), [&](int l, int r){ return T[l] < T[r]; }); vector iTI(N); rep(i,N) iTI[TI[i]] = i; { vector H2(N); rep(i,N) H2[i] = H[TI[i]]; swap(H, H2); sort(T.begin(), T.end()); } vector HI = TI; sort(HI.begin(), HI.end(), [&](int l, int r){ return H[l] < H[r]; }); vector highT(N+1); highT[0] = -1; rep(i,N) highT[i+1] = max(highT[i], HI[i]); vector to(N); rep(i,N) to[i] = max(i, highT[lower_bound(HI.begin(), HI.end(), T[i], [&](int hi, int t){ return H[hi] <= t; }) - HI.begin()]); vector> dbl(20, vector(N)); dbl[0] = to; rep(t,19) rep(i,N) dbl[t+1][i] = (dbl[t][i] == -1 ? -1 : dbl[t][dbl[t][i]]); int Q; cin >> Q; rep(q,Q){ int a, b; cin >> a >> b; a--; b--; a = iTI[a]; b = iTI[b]; if(T[a] >= H[b]){ cout << "1\n"; continue; } int res = 0; int at = a; for(int t=19; t>=0; t--) if(T[dbl[t][at]] < H[b]){ at = dbl[t][at]; res += 1 << t; } if(res >= 1000000){ cout << "-1\n"; continue; } cout << (res+2) << '\n'; } return 0; } struct ios_do_not_sync{ ios_do_not_sync(){ ios::sync_with_stdio(false); cin.tie(nullptr); } } ios_do_not_sync_instance;