#P1719C. Fighting Tournament

    ID: 865 Type: RemoteJudge 2000ms 256MiB Tried: 0 Accepted: 0 Difficulty: 4 Uploaded By: Tags>binary searchdata structuresimplementationtwo pointers*1400

Fighting Tournament

Description

Burenka is about to watch the most interesting sporting event of the year — a fighting tournament organized by her friend Tonya.

nn athletes participate in the tournament, numbered from 11 to nn. Burenka determined the strength of the ii-th athlete as an integer aia_i, where 1ain1 \leq a_i \leq n. All the strength values are different, that is, the array aa is a permutation of length nn. We know that in a fight, if ai>aja_i > a_j, then the ii-th participant always wins the jj-th.

The tournament goes like this: initially, all nn athletes line up in ascending order of their ids, and then there are infinitely many fighting rounds. In each round there is exactly one fight: the first two people in line come out and fight. The winner goes back to the front of the line, and the loser goes to the back.

Burenka decided to ask Tonya qq questions. In each question, Burenka asks how many victories the ii-th participant gets in the first kk rounds of the competition for some given numbers ii and kk. Tonya is not very good at analytics, so he asks you to help him answer all the questions.

The first line contains one integer tt (1t1041 \leq t \leq 10^4) — the number of test cases. Description of the test cases follows.

The first line of each test case contains two integers nn and qq (2n1052 \leq n \leq 10^5, 1q1051 \leq q \leq 10^5) — the number of tournament participants and the number of questions.

The second line of each test case contains nn integers a1,a2,,ana_1, a_2, \ldots, a_n (1ain1 \leq a_i \leq n) — the array aa, which is a permutation.

The next qq lines of a test case contain questions. Each line contains two integers ii and kk (1in1 \leq i \leq n, 1k1091 \leq k \leq 10^9) — the number of the participant and the number of rounds.

It is guaranteed that the sum of nn and the sum of qq over all test cases do not exceed 10510^5.

For each Burenka's question, print a single line containing one integer — the answer to the question.

Input

The first line contains one integer tt (1t1041 \leq t \leq 10^4) — the number of test cases. Description of the test cases follows.

The first line of each test case contains two integers nn and qq (2n1052 \leq n \leq 10^5, 1q1051 \leq q \leq 10^5) — the number of tournament participants and the number of questions.

The second line of each test case contains nn integers a1,a2,,ana_1, a_2, \ldots, a_n (1ain1 \leq a_i \leq n) — the array aa, which is a permutation.

The next qq lines of a test case contain questions. Each line contains two integers ii and kk (1in1 \leq i \leq n, 1k1091 \leq k \leq 10^9) — the number of the participant and the number of rounds.

It is guaranteed that the sum of nn and the sum of qq over all test cases do not exceed 10510^5.

Output

For each Burenka's question, print a single line containing one integer — the answer to the question.

Sample Input 1

3
3 1
3 1 2
1 2
4 2
1 3 4 2
4 5
3 2
5 2
1 2 3 5 4
5 1000000000
4 6

Sample Output 1

2
0
1
0
4

Note

In the first test case, the first numbered athlete has the strength of 33, in the first round he will defeat the athlete with the number 22 and the strength of 11, and in the second round, the athlete with the number 33 and the strength of 22.

In the second test case, we list the strengths of the athletes fighting in the first 55 fights: 11 and 33, 33 and 44, 44 and 22, 44 and 11, 44 and 33. The participant with the number 44 in the first 55 rounds won 00 times (his strength is 22). The participant with the number 33 has a strength of 44 and won 11 time in the first two fights by fighting 11 time.