Under construction

Still being built. Things may change

Bob's Average

Time limit3sMemory limit1024 MB

Summary
For each query on an odd-length subarray, find the largest single value that repeated median-of-three merges can leave behind.
Level

Hard8 of 10

Topics
Binary search, Prefix sum, Segment tree
Solved
No attempts yet

Problem

Bob came up with a new way to compute the average of an array with an odd number of elements. While the length of the array is greater than one, the following operation is performed: an arbitrary segment of length 3 with boundaries ll and r=l+2r = l + 2 is selected, the median of these three elements is computed, and the three elements are replaced by one element equal to the median. The median of three elements is the second-largest of them. For example, the median of [1,5,4][1, 5, 4] is 44, and the median of [2,2,2][2, 2, 2] is 22.

For the array [4,1,3,2,5][4, 1, 3, 2, 5], choose l=2l = 2, r=4r = 4. The median of [1,3,2][1, 3, 2] is 22, so the array becomes [4,2,5][4, 2, 5]. Then choose l=1l = 1, r=3r = 3. The median of [4,2,5][4, 2, 5] is 44, so the array becomes [4][4].

The result depends on the choice of segments. Bob chooses the segments so that the single number left at the end is as large as possible. This number is called Bob's average for the array.

For an array aa of nn elements, you must answer qq queries. The jj-th query gives LjL_j and RjR_j, and the answer is Bob's average of the subarray [aLj,aLj+1,…,aRj][a_{L_j}, a_{L_j+1}, \ldots, a_{R_j}]. Every query has an odd segment length.

Input

The first line contains an integer nn (3≤n≤5⋅1043 \leq n \leq 5 \cdot 10^4), the size of the array.

The second line contains nn integers aia_i (0≤ai≤1090 \leq a_i \leq 10^9), the array elements.

The third line contains an integer qq (1≤q≤1051 \leq q \leq 10^5), the number of queries.

Each of the next qq lines contains two integers LjL_j and RjR_j (1≤Lj≤Rj≤n1 \leq L_j \leq R_j \leq n). It is guaranteed that Rj−Lj+1R_j - L_j + 1 is odd.

Output

For each query, print on its own line the answer to that query, a single integer.

Examples1

  1. Example 1

    Input
    7
    4 1 3 2 5 1 7
    5
    2 6
    1 1
    1 5
    3 7
    1 7
    
    Expected output
    2
    4
    4
    3
    4