#Q124. 「一本通 4.2 练习 1」天才的记忆

「一本通 4.2 练习 1」天才的记忆

Description

Original source: Vijos P1512

Once upon a time, there was a person named W and N and B, who possessed a genius-level memory and collected numerous treasures. After his passing, he left behind a puzzle (specifically designed to test memory!) for future generations. Whoever could easily answer this question would inherit his treasures.

The problem is as follows: You are given a long string of numbers (numbered from 11 to NN, and their sizes may vary!). After you have seen them once, they disappear before your eyes, and then the question arises: You are given MM queries, each query provides two numbers AA and BB, and you must instantly state the maximum number within the interval from AA to BB.

One day, a beautiful sister flew by from the sky, saw this problem, and found it very interesting (mainly because it was said that the treasure contained a kind of beauty potion that could make this beautiful sister even more charming). So she tried her best to solve this problem. BUT, she failed every time because the number of digits was simply too large! So she asked you, the genius, to help her solve it. If you help her solve this problem, you will receive many sweet rewards!

Input Format

The first line contains an integer NN representing the number of digits. The next line contains NN numbers. The third line contains an integer MM, indicating the number of times you will be questioned after seeing the string of numbers. The following MM lines each contain two integers AA and BB.

Output Format

Output MM lines, each containing a number representing the answer to a query.

Sample 1

6
34 1 8 123 3 2
4
1 2
1 5
3 4
2 3

34
123
123
8

Data Range and Hints

For 30%30\% of the data, 1N104,1M1001\le N\le 10^4,1\le M\le 100;

For 100%100\% of the data, 1N2×105,1M1041\le N\le 2\times 10^5,1\le M\le 10^4.