#include #include #include #include #include #include #include #include #include #include #include #include #include #define debug_value(x) cerr << "line" << __LINE__ << ":<" << __func__ << ">:" << #x << "=" << x << endl; #define debug(x) cerr << "line" << __LINE__ << ":<" << __func__ << ">:" << x << endl; template inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; } template inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; } using namespace std; typedef long long ll; template vector> vec2d(int n, int m, T v){ return vector>(n, vector(m, v)); } template vector>> vec3d(int n, int m, int k, T v){ return vector>>(n, vector>(m, vector(k, v))); } template void print_vector(vector v, char delimiter=' '){ if(v.empty()) { cout << endl; return; } for(int i = 0; i+1 < v.size(); i++) cout << v[i] << delimiter; cout << v.back() << endl; } template class Compress{ public: vector data; int offset; Compress(vector data_, int offset=0): offset(offset){ data = data_; sort(begin(data), end(data)); data.erase(unique(begin(data), end(data)), end(data)); }; int operator[](T x) { auto p = lower_bound(data.begin(), data.end(), x); assert(x == *p); return offset+(p-data.begin()); } T inv(int x){ return data[x-offset]; } int size(){ return data.size(); } }; int main(){ ios::sync_with_stdio(false); cin.tie(0); cout << setprecision(10) << fixed; 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 v = h; for(int x: t) v.push_back(x); auto cp = Compress(v); int m = cp.size(); vector mx(m, -1); for(int i = 0; i < n; i++){ int j = cp[h[i]]; chmax(mx[j], cp[t[i]]); } for(int i = 1; i < m; i++) chmax(mx[i], mx[i-1]); auto nx = vec2d(20, n+m, -1); for(int i = 0; i < m; i++){ nx[0][i] = mx[i]; } for(int j = 1; j < 20; j++){ for(int i = 0; i < m; i++){ if(nx[j-1][i] == -1){ nx[j][i] = -1; }else{ nx[j][i] = max(nx[j-1][nx[j-1][i]], nx[j-1][i]); } } } int q; cin >> q; while(q--){ int a, b; cin >> a >> b; a--; b--; if(t[a] >= h[b]){ cout << 1 << endl; continue; } int s = cp[t[a]]; int t = cp[h[b]]; int ans = 1; int cur = s; if(cur == -1){ cout << -1 << endl; continue; } int max_go = cur; for(int j = 19; j >= 0; j--){ max_go = max_go == -1 ? max_go : nx[j][max_go]; } if(max_go < t){ cout << -1 << endl; continue; } for(int j = 19; j >= 0; j--){ if(nx[j][cur] >= t){ }else{ ans += 1<