#P1602B. Divine Array

    ID: 1633 Type: RemoteJudge 2000ms 256MiB Tried: 0 Accepted: 0 Difficulty: 3 Uploaded By: Tags>constructive algorithmsimplementation*1100

Divine Array

Description

Black is gifted with a Divine array aa consisting of nn (1n20001 \le n \le 2000) integers. Each position in aa has an initial value. After shouting a curse over the array, it becomes angry and starts an unstoppable transformation.

The transformation consists of infinite steps. Array aa changes at the ii-th step in the following way: for every position jj, aja_j becomes equal to the number of occurrences of aja_j in aa before starting this step.

Here is an example to help you understand the process better:

Initial array:22 11 11 44 33 11 22
After the 11-st step:22 33 33 11 11 33 22
After the 22-nd step:22 33 33 22 22 33 22
After the 33-rd step:44 33 33 44 44 33 44
......

In the initial array, we had two 22-s, three 11-s, only one 44 and only one 33, so after the first step, each element became equal to the number of its occurrences in the initial array: all twos changed to 22, all ones changed to 33, four changed to 11 and three changed to 11.

The transformation steps continue forever.

You have to process qq queries: in each query, Black is curious to know the value of axa_x after the kk-th step of transformation.

Each test contains multiple test cases. The first line contains the number of test cases tt (1t10001 \le t \le 1000). Description of the test cases follows.

The first line of each test case contains an integer nn (1n20001 \le n \le 2000) — the size of the array aa.

The second line of each test case contains nn integers a1,a2,,ana_1, a_2, \dots, a_n (1ain1 \le a_i \le n) — the initial values of array aa.

The third line of each test case contains a single integer qq (1q1000001 \le q \le 100\,000) — the number of queries.

Next qq lines contain the information about queries — one query per line. The ii-th line contains two integers xix_i and kik_i (1xin1 \le x_i \le n; 0ki1090 \le k_i \le 10^9), meaning that Black is asking for the value of axia_{x_i} after the kik_i-th step of transformation. ki=0k_i = 0 means that Black is interested in values of the initial array.

It is guaranteed that the sum of nn over all test cases doesn't exceed 20002000 and the sum of qq over all test cases doesn't exceed 100000100\,000.

For each test case, print qq answers. The ii-th of them should be the value of axia_{x_i} after the kik_i-th step of transformation. It can be shown that the answer to each query is unique.

Input

Each test contains multiple test cases. The first line contains the number of test cases tt (1t10001 \le t \le 1000). Description of the test cases follows.

The first line of each test case contains an integer nn (1n20001 \le n \le 2000) — the size of the array aa.

The second line of each test case contains nn integers a1,a2,,ana_1, a_2, \dots, a_n (1ain1 \le a_i \le n) — the initial values of array aa.

The third line of each test case contains a single integer qq (1q1000001 \le q \le 100\,000) — the number of queries.

Next qq lines contain the information about queries — one query per line. The ii-th line contains two integers xix_i and kik_i (1xin1 \le x_i \le n; 0ki1090 \le k_i \le 10^9), meaning that Black is asking for the value of axia_{x_i} after the kik_i-th step of transformation. ki=0k_i = 0 means that Black is interested in values of the initial array.

It is guaranteed that the sum of nn over all test cases doesn't exceed 20002000 and the sum of qq over all test cases doesn't exceed 100000100\,000.

Output

For each test case, print qq answers. The ii-th of them should be the value of axia_{x_i} after the kik_i-th step of transformation. It can be shown that the answer to each query is unique.

Sample Input 1

2
7
2 1 1 4 3 1 2
4
3 0
1 1
2 2
6 1
2
1 1
2
1 0
2 1000000000

Sample Output 1

1
2
3
3
1
2

Note

The first test case was described ih the statement. It can be seen that:

  1. k1=0k_1 = 0 (initial array): a3=1a_3 = 1;
  2. k2=1k_2 = 1 (after the 11-st step): a1=2a_1 = 2;
  3. k3=2k_3 = 2 (after the 22-nd step): a2=3a_2 = 3;
  4. k4=1k_4 = 1 (after the 11-st step): a6=3a_6 = 3.

For the second test case,

Initial array:11 11
After the 11-st step:22 22
After the 22-nd step:22 22
......

It can be seen that:

  1. k1=0k_1 = 0 (initial array): a1=1a_1 = 1;
  2. k2=1000000000k_2 = 1000000000: a2=2a_2 = 2;