14957 - Subsequence Mode   

Description

The mode of a sequence is a value that appears most frequently in the sequence.
If there are multiple such values, the maximum mode is the largest among them.

 

Given a sequence A = a1, ..., aN, answer Q queries.

In the i-th query, you are given two integers Li and Ri, and have to find the maximum mode of A[Li...Ri].

It is guaranteed for each i = 1, ..., Q - 1, LiLi+1 and RiRi+1.

 

scanf and printf are recommended over cin and cout to speed up input and output.

 

Input

The first line of the input contains an integer T, the number of testcases.

The first line of each testcase contains two integers N and Q, the length of A and the number of queries.

The second line of each testcase contains N integers, a1, ..., aN, the sequence A.

Each of the next Q lines contains two integers Li and Ri, the range of the subsequence in the i-th query.

 

Constraints

There are six subtasks. For each subtask,

  1. N ≤ 20, Q ≤ 10
  2. N ≤ 20, Q ≤ 10
  3. N ≤ 2000, Q ≤ 1000
  4. N ≤ 2000, Q ≤ 1000
  5. N ≤ 200000, Q ≤ 100000
  6. N ≤ 200000, Q ≤ 100000

In every subtask, 1 ≤ T ≤ 10, 1 ≤ ai ≤ 109, 1 ≤ LiRiN.

 

Output

For each query in each testcase, please print the maximum mode of A[Li...Ri] followed by a newline.

 

Sample Input  Download

Sample Output  Download




Discuss