#P4168. Dandelion Query

    ID: 17415 Type: Default 1000ms 256MiB

Dandelion Query

Dandelion Query

In the countryside, along the roadside, many dandelions are planted. For simplicity, we consider all dandelions as a sequence of length nn, given by $${a_1,a_2,\dots,a_n}$$, where each aia_i is a positive integer representing the species identifier of the ii-th dandelion.

For each query with an interval [l,r][l, r], you need to output the species that appears most frequently in that interval. If there are multiple species with the same frequency, output the one with the smallest identifier.

Note: Your algorithm must be online.

inputFormat

The first line contains an integer nn --- the number of dandelions. The second line contains nn positive integers a1,a2,,ana_1, a_2, \dots, a_n. The third line contains an integer qq --- the number of queries. Each of the next qq lines contains two integers ll and rr, representing a query interval.

outputFormat

For each query, output the species identifier that appears most frequently within the interval on a new line. In case of a tie, output the smallest species identifier.

sample

5
1 1 2 2 3
3
1 5
1 2
4 5
1

1 2

</p>