Smallest unreachable subsequence sum

For each subarray, find the smallest non-negative integer that no subsequence sums to.

Hard9Segment treeGreedySortingNo attempts yetTime limit2sMemory limit512 MB

Problem

You are given a sequence AA of length NN. A subsequence is what is left after deleting some elements of AA, and deleting every element is allowed, so the empty sequence is a subsequence too. The sum of a subsequence is the sum of the integers it keeps, and the sum of the empty subsequence is 0.

For example, if AA is [1, 1, 3, 7], the subsequences [], [1], [1, 1], [3], [1, 3], [1, 1, 3] have sums 0, 1, 2, 3, 4, 5 in that order. No subsequence sums to 6, so the smallest non-negative integer that is not a subsequence sum of AA is 6.

Then MM queries are given. Each query consists of two integers LL and RR and asks for the smallest non-negative integer that is not the sum of any subsequence of AL,AL+1,,ARA_L, A_{L+1}, \dots, A_R. Write a program that answers every query.

Input

The first line contains the length of the sequence NN (1N1000001 \le N \le 100000). The second line contains the sequence A1,A2,,ANA_1, A_2, \dots, A_N. Every number is a natural number at most 10910^9, and the sum of all numbers in the sequence is also at most 10910^9.

The third line contains the number of queries MM (1M1000001 \le M \le 100000). Each of the next MM lines contains one query in the form LiL_i RiR_i (1LiRiN1 \le L_i \le R_i \le N).

Output

Print the answer to each query on its own line, in the order the queries are given.