#include #include #include #include int main() { int n; std::cin >> n; int log = 1; while ((1 << log) < n) { log++; } std::vector h(n), t(n); std::vector> event; event.reserve(2 * n); for (int i = 0; i < n; i++) { std::cin >> h[i]; event.emplace_back(h[i], ~i); } for (int i = 0; i < n; i++) { std::cin >> t[i]; event.emplace_back(t[i], i); } std::sort(event.begin(), event.end()); std::vector> parent(n, std::vector(log + 1, -1)); int max = -1; int max_index = -1; for (auto [x, i]: event) { // h if (i < 0) { i = ~i; if (max < t[i]) { max = t[i]; max_index = i; } } else { // t if (x < max) { parent[i][0] = max_index; } } } for (int j = 0; j < log; j++) { for (int i = 0; i < n; i++) { int mid_parent = parent[i][j]; if (mid_parent >= 0) { parent[i][j + 1] = parent[mid_parent][j]; } } } int q; std::cin >> q; while (q--) { int a, b; std::cin >> a >> b; a--; b--; // a -> b // h[b] <= t[a]となるまでdoublingで上がっていく int count = 0; for (int i = log; i >= 0; i--) { if (parent[a][i] != -1) { if (t[parent[a][i]] < h[b]) { a = parent[a][i]; count += (1 << i); } } } if (h[b] <= t[a]) { std::cout << 1 << '\n'; } else if (parent[a][0] == -1) { std::cout << -1 << '\n'; } else { std::cout << count + 2 << '\n'; } } }