You are given an array A of size N. Let us list down all the subarrays of the given array. There will be a total of N * (N + 1) / 2 subarrays of the given array. Let us sort each of the subarrays in descending order of the numbers in it. 

Now you want to sort these subarrays in descending order. You can compare two subarrays B, C, as follows.

compare(B, C):
Append N - |B| zeros at the end of the array B.
Append N - |C| zeros at the end of the array C.
for i = 1 to N:
if B[i] < C[i]:
return B is less than C
if B[i] > C[i]:
return B is greater than C
return B and C are equal.

You are given M queries asking for the maximum element in the pth subarray (1-based indexing).

-----Input-----
The first line of input contains T, the number of test cases.
The first line of each test case contains two space separated integers N and M, denoting the array size and the number of queries respectively.
The next line contains N space-separated integers denoting the array elements.

Each of the next M lines contains a single integer - p.

-----Output-----
Output a single integer corresponding to the maximum element in the pth subarray.

-----Constraints-----
- 1 ≤ Ai ≤ 109
- 1 ≤ p ≤ N+1C2

-----Subtasks-----Subtask #1 (20 points):
- 1 ≤ T ≤ 20
- 1 ≤ N ≤ 200
- 1 ≤ M ≤ 104
Subtask #2 (30 points):
- 1 ≤ T ≤ 20
- 1 ≤ N ≤ 3000
- 1 ≤ M ≤ 104
Subtask #3 (50 points):
- 1 ≤ T ≤ 5
- 1 ≤ N ≤ 105
- 1 ≤ M ≤ 105

-----Example-----
Input:1
4 2
3 1 2 4
1
5

Output:4
3

Difficulty: interview

Starter code:

Solutions:
# cook your dish here
for _ in range(int(input())):
 n,q=map(int,input().split())
 l=[int(i) for i in input().split()]
 qry=[int(input()) for i in range(q)]
 def cmp(sub1,sub2):
  for i in range(len(sub1)):
   if sub1[i]>sub2[i]:
    return 1 
   if sub1[i]<sub2[i]:
    return 2 
  return 1 
 maxl=[]
 for i in range(n):
  for j in range(i,n):
   maxl.append(max(l[i:j+1]))
 maxl.sort(reverse=True)
 for i in qry:
  print(maxl[i-1])

for _ in range(int(input())):
 N, M = map(int, input().split())
 arr = list(map(int, input().split()))
 arr1 = sorted(arr, reverse=True)
 lst = []
 for i in range(N):
  for j in range(i, N):
   x = arr[i:j+1]
   x.sort(reverse=True)
   x.extend([0]*(N-len(x)))
   lst.append(x)
 lst.sort(reverse=True)
  
 for j in range(M):
  p = int(input())-1
  if p == N*(N+1)//2:
   print(min(arr))
  else:
   print(lst[p][0])

for _ in range(int(input())):
 N, M = map(int, input().split())
 arr = list(map(int, input().split()))
 arr1 = sorted(arr, reverse=True)
 lst = []
 for i in range(N):
  for j in range(i, N):
   x = arr[i:j+1]
   x.sort(reverse=True)
   x.extend([0]*(N-len(x)))
   lst.append(x)
 lst.sort(reverse=True)
  
 for j in range(M):
  p = int(input())-1
  print(lst[p][0])

for _ in range(int(input())):
 n,q=map(int,input().split())
 l=[int(i) for i in input().split()]
 qry=[int(input()) for i in range(q)]
 def cmp(sub1,sub2):
  for i in range(len(sub1)):
   if sub1[i]>sub2[i]:
    return 1 
   if sub1[i]<sub2[i]:
    return 2 
  return 1 
 maxl=[]
 for i in range(n):
  for j in range(i,n):
   maxl.append(max(l[i:j+1]))
 maxl.sort(reverse=True)
 for i in qry:
  print(maxl[i-1])

for _ in range(int(input())):
 n,q=map(int,input().split())
 l=[int(i) for i in input().split()]
 qry=[int(input()) for i in range(q)]
 def cmp(sub1,sub2):
  for i in range(len(sub1)):
   if sub1[i]>sub2[i]:
    return 1 
   if sub1[i]<sub2[i]:
    return 2 
  return 1 
 maxl=[]
 for i in range(n):
  for j in range(i,n):
   maxl.append(max(l[i:j+1]))
 maxl.sort(reverse=True)
 for i in qry:
  print(maxl[i-1])