import bisect def main(): import sys data = sys.stdin.read().split() ptr = 0 N = int(data[ptr]) ptr += 1 H = list(map(int, data[ptr:ptr+N])) ptr += N T = list(map(int, data[ptr:ptr+N])) ptr += N Q = int(data[ptr]) ptr += 1 queries = [] for _ in range(Q): A = int(data[ptr]) - 1 B = int(data[ptr+1]) - 1 queries.append((A, B)) ptr += 2 # Sort cities by their height sorted_HT = sorted(zip(H, T), key=lambda x: x[0]) sorted_H = [ht[0] for ht in sorted_HT] sorted_T = [ht[1] for ht in sorted_HT] # Compute prefix maximum array for T values prefix_max = [0] * N prefix_max[0] = sorted_T[0] for i in range(1, N): prefix_max[i] = max(prefix_max[i-1], sorted_T[i]) output = [] for A, B in queries: h_b = H[B] t_a = T[A] if h_b <= t_a: output.append('1') continue current_m = t_a steps = 1 while True: idx = bisect.bisect_right(sorted_H, current_m) - 1 if idx < 0: next_m = -1 else: next_m = prefix_max[idx] if next_m == current_m: output.append('-1') break steps += 1 current_m = next_m if current_m >= h_b: output.append(str(steps)) break print('\n'.join(output)) if __name__ == '__main__': main()