14963 - Another 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 k-th largest one is the k-th largest value among them.
For example, the 1st largest mode of the sequence [1, 2, 2, 3, 3] is 3, the 2nd largest mode is 2, and the 3rd largest mode does not exist.

 

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

In the i-th query, you are given three integers LiRi, and Ki, and have to find the Ki-th largest mode of A[Li...Ri].

It is guaranteed for each i = 1, ..., Q - 1, Li - 3 ≤ Li+1 and Ri - 3 ≤ Ri+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 three integers Li, Ri, and Ki, the range and the order of the mode to report for the i-th query.

 

Constraints

There are five subtasks. For each subtask,

  1. N ≤ 2000, Q ≤ 1000.
  2. N ≤ 200000, Q ≤ 100000, Ki = 1 for every query, and LiLi+1 and RiRi+1 for each i = 1, ..., Q - 1.
  3. N ≤ 200000, Q ≤ 100000, and LiLi+1 and RiRi+1 for each i = 1, ..., Q - 1.
  4. N ≤ 200000, Q ≤ 100000, and Ki = 1 for every query.
  5. N ≤ 200000, Q ≤ 100000.

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

 

Output

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

If such value does not exist, print -1 instead.

 

Sample Input  Download

Sample Output  Download

Tags




Discuss