CSES - Couriers
  • Time limit: 2.50 s
  • Memory limit: 128 MB

Byteasar works for the BAJ company, which sells computer games. The BAJ company cooperates with many courier companies that deliver the games sold by the BAJ company to its customers. Byteasar is inspecting the cooperation of the BAJ company with the couriers. He has a log of successive packages with the courier company that made the delivery specified for each package. He wants to make sure that no courier company had an unfair advantage over the others.

If a given courier company delivered more than half of all packages sent in some period of time, we say that it dominated in that period. Byteasar wants to find out which courier companies dominated in certain periods of time, if any.

Help Byteasar out! Write a program that determines a dominating courier company or that there was none.

Input

The first line of the standard input contains two integers, nn and mm, separated by a single space, that are the number of packages shipped by the BAJ company and the number of time periods for which the dominating courier is to be determined, respectively. The courier companies are numbered from 11 to (at most) nn.

The second line of input contains nn integers, p1,p2,,pnp_1, p_2, \ldots, p_n, separated by single spaces; pip_i is the number of the courier company that delivered the ii-th package (in shipment chronology).

The mm lines that follow specify the time period queries, one per line. Each query is specified by two integers, aia_i and bib_i, separated by a single space. These mean that the courier company dominating in the period between the shipments of the aia_i-th and the bib_i-th package, including those, is to be determined.

Output

The answers to successive queries should be printed to the standard output, one per line. (Thus a total of mm lines should be printed.) Each line should hold a single integer: the number of the courier company that dominated in the corresponding time period, or 00 if there was no such company.

Constraints

  • 1n,m51051 \le n, m \le 5 \cdot 10^5
  • 1aibin1 \le a_i \le b_i \le n

Example

For the input data:

7 5
1 1 3 2 3 4 3
1 3
1 4
3 7
1 7
6 6

the correct result is:

1
0
3
0
4

Task author: Jacek Tomasiewicz.