#include using namespace std; int main() { int n; cin >> n; vector h(n), t(n); for (int i = 0; i < n; i++) { cin >> h.at(i); } for (int i = 0; i < n; i++) { cin >> t.at(i); } int q; cin >> q; vector a(q), b(q); for (int i = 0; i < q; i++) { cin >> a.at(i) >> b.at(i); a.at(i)--; b.at(i)--; } auto h_copy = h; sort(h_copy.begin(), h_copy.end()); h_copy.erase(unique(h_copy.begin(), h_copy.end()), h_copy.end()); for (int i = 0; i < n; i++) { h.at(i) = upper_bound(h_copy.begin(), h_copy.end(), h.at(i)) - h_copy.begin(); t.at(i) = upper_bound(h_copy.begin(), h_copy.end(), t.at(i)) - h_copy.begin(); } const int m = h_copy.size(); constexpr int l = 20; vector> highest(l, vector(m + 1, 0)); for (int i = 0; i < n; i++) { highest.at(0).at(h.at(i)) = max(highest.at(0).at(h.at(i)), t.at(i)); } for (int i = 0; i < m; i++) { highest.at(0).at(i + 1) = max(highest.at(0).at(i + 1), highest.at(0).at(i)); } for (int i = 0; i < l - 1; i++) { for (int j = 0; j <= m; j++) { highest.at(i + 1).at(j) = highest.at(i).at(highest.at(i).at(j)); } } for (int i = 0; i < q; i++) { const int ta = t.at(a.at(i)), hb = h.at(b.at(i)); if (highest.back().at(ta) < hb) { puts("-1"); continue; } if (ta >= hb) { puts("1"); continue; } int ans = 2, x = ta; while (x < hb) { int k = 0; while (highest.at(k).at(x) < hb) { k++; } if (k == 0) break; k--; x = highest.at(k).at(x); ans += 1 << k; } cout << ans << "\n"; } return 0; }