"If you didn't copy assignments during your engineering course, did you even do engineering?"
There are $Q$ students in Chef's class. Chef's teacher has given the students a simple assignment:
Write a function that takes as arguments an array $A$ containing only unique elements and a number $X$ guaranteed to be present in the array and returns the ($1$-based) index of the element that is equal to $X$.
The teacher was expecting a linear search algorithm, but since Chef is such an amazing programmer, he decided to write the following binary search function:
integer binary_search(array a, integer n, integer x):
integer low, high, mid
low := 1
high := n
while low ≤ high:
mid := (low + high) / 2
if a[mid] == x:
break
else if a[mid] is less than x:
low := mid+1
else:
high := mid-1
return mid

All of Chef's classmates have copied his code and submitted it to the teacher.
Chef later realised that since he forgot to sort the array, the binary search algorithm may not work. Luckily, the teacher is tired today, so she asked Chef to assist her with grading the codes. Each student's code is graded by providing an array $A$ and an integer $X$ to it and checking if the returned index is correct. However, the teacher is lazy and provides the exact same array to all codes. The only thing that varies is the value of $X$.
Chef was asked to type in the inputs. He decides that when typing in the input array for each code, he's not going to use the input array he's given, but an array created by swapping some pairs of elements of this original input array. However, he cannot change the position of the element that's equal to $X$ itself, since that would be suspicious.
For each of the $Q$ students, Chef would like to know the minimum number of swaps required to make the algorithm find the correct answer.

-----Input-----
- The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows.
- The first line of each test case contains two space-separated integers $N$ and $Q$ denoting the number of elements in the array and the number of students.
- The second line contains $N$ space-separated integers $A_1, A_2, \dots, A_N$.
- The following $Q$ lines describe queries. Each of these lines contains a single integer $X$.

-----Output-----
For each query, print a single line containing one integer — the minimum required number of swaps, or $-1$ if it is impossible to make the algorithm find the correct answer. (Do you really think Chef can fail?)

-----Constraints-----
- $1 \le T \le 10$
- $1 \le N, Q \le 10^5$
- $1 \le A_i \le 10^9$ for each valid $i$
- $1 \le X \le 10^9$
- all elements of $A$ are pairwise distinct
- for each query, $X$ is present in $A$
- sum of $N$ over all test cases $\le 5\cdot10^5$
- sum of $Q$ over all test cases $\le 5\cdot10^5$

-----Subtasks-----
Subtask #1 (20 points): $1 \le N \le 10$
Subtask #2 (30 points):
- $1 \le A_i \le 10^6$ for each valid $i$
- $1 \le X \le 10^6$
Subtask #3 (50 points): original constraints

-----Example Input-----
1
7 7
3 1 6 7 2 5 4
1
2
3
4
5
6
7

-----Example Output-----
0
1
1
2
1
0
0

-----Explanation-----
Example case 1:
- Query 1: The algorithm works without any swaps.
- Query 2: One solution is to swap $A_2$ and $A_4$.
- Query 3: One solution is to swap $A_2$ and $A_6$.
- Query 4: One solution is to swap $A_2$ with $A_4$ and $A_5$ with $A_6$.
- Query 5: One solution is to swap $A_2$ and $A_4$.
- Query 6: The algorithm works without any swaps.
- Query 7: The algorithm works without any swaps.

Starter code:

Solutions:
def f(a,y,index,sorted_pos):
 #print(a,y,index,sorted_pos)
 n=len(a)
 low=0
 high=n-1
 L,R=0,0
 l,r=0,0
 while(low<=high):
  mid=(low+high)//2
  #print(low,high,mid)
  if(a[mid]== y):
   break
  elif(mid > index[y]):
   high=mid-1
   L+=1
   #print("L")
   if(a[mid] <y):
    l+=1
    #print(" l ")
  else:
   low=mid+1
   R+=1
   #print("R")
   if(a[mid]>y):
    r+=1
    #print("r")
 x=sorted_pos[y]
 #print(L,R,l,r,x,n-x-1)
 if(R>x or L> n-x-1):
  print("-1")
 else:
  print(max(l,r))


def fun():
 test=int(input())
 for t in range(test):
  n,q=list(map(int,input().split()))
  arr=list(map(int,input().split()))
  index= dict()
  for i in range(n):
   index[arr[i]]=i
  sorted_pos=dict()
  a=sorted(arr)
  for i in range(n):
   sorted_pos[a[i]]=i
  for x in range(q):
   y=int(input())
   f(arr,y,index,sorted_pos)

fun()



from sys import stdin,stdout
t=int(stdin.readline())
while(t):
 n,q=list(map(int,stdin.readline().rstrip().split()))
 arr=list(map(int,stdin.readline().rstrip().split()))
 d={}
 d1={}
 a=arr.copy()
 a.sort()
 for i in range(n):
  d1[a[i]]=i
  d[arr[i]]=i
 while(q):
  v=int(stdin.readline().rstrip())
  index=d[v]
  smaller=d1[v]
  bigger=n-smaller-1
  low,high=0,n-1
  c1,c2=0,0
  while(high>=low):
   mid=(high+low)//2
   if mid==index:
    break
   elif index>mid:
    if arr[mid]>v:
     c1+=1
    else:
     smaller-=1
    low=mid+1
   else:
    if v>arr[mid]:
     c2+=1
    else:
     bigger-=1
    high=mid-1
  if c2>c1:
   if bigger>=(c2-c1):
    print(c2)
   else:
    print(-1)
  elif c1>c2:
   if smaller>=(c1-c2):
    print(c1)
   else:
    print(-1)
  else:
   print(c1)
  q-=1
 t-=1



def binarySearch(start, end, xindex,opList):
 if start <= end:
  midindex = int((start+end)/2)
  opList.append(midindex)
  if xindex < midindex:
   return binarySearch(start, midindex-1, xindex, opList)

  elif xindex > midindex:
   return binarySearch(midindex+1, end, xindex, opList)

  else:
   return opList


def counter(opList,a, x):
 small = 0
 big = 0
 rightSmall = 0
 rightBig = 0

 for i in range(len(opList)-1):
  # print("for oplist[",i,"]=",opList[i])
  if opList[i] < opList[i+1]:
   # print("traverse to right")
   if x < a[opList[i]]:
    # print("x is small")
    small += 1
   else:
    rightSmall += 1
  else:
   # print("traverse to left")
   if x> a[opList[i]]:
    # print("x is big")
    big += 1
   else:
    rightBig += 1
 flag = isBigSmallAvailable(small,big,x,a,rightBig,rightSmall)
 if flag:
  total = small + big
  total = total - min(small,big)
  return total
 else:
  return -1


def isBigSmallAvailable(small,big,x,a,rightBig,rightSmall):
 s = 0
 b = 0
 s = s - rightSmall
 b = b - rightBig
 length = len(a)
 for i in range(length):
  if a[i] < x:
   s += 1
  if a[i] > x:
   b += 1

  if s >= small and b >= big:
   return True
 return False


t = int(input())
for z in range(t):
 l = list(map(int,input().split()))
 n = int(l[0])
 q = int(l[1])
 a = list(map(int,input().split()))
 for i in range(q):
  x = int(input())
  xindex = a.index(x)
  opList = []
  result = binarySearch(0, n-1, xindex, opList)
  count = counter(opList, a, x)

  # print(result)
  print(count)
  # else:
  #     print(swapCount)


def binarySearch(start, end, xindex,opList):
 if start <= end:
  midindex = int((start+end)/2)

  opList.append(midindex)
  if xindex < midindex :
   return binarySearch(start, midindex-1, xindex, opList)

  elif xindex > midindex :
   return binarySearch(midindex+1, end, xindex, opList)

  else:
   return opList


def counter(opList,a, x):
 small = 0
 big = 0
 rightSmall = 0
 rightBig = 0

 for i in range(len(opList)-1):
  # print("for oplist[",i,"]=",opList[i])
  if opList[i] < opList[i+1]:
   # print("traverse to right")
   if x < a[opList[i]]:
    # print("x is small")
    small += 1
   else:
    rightSmall += 1
  else:
   # print("traverse to left")
   if x> a[opList[i]]:
    # print("x is big")
    big += 1
   else:
    rightBig += 1
 flag = isBigSmallAvailable(small,big,x,a,rightBig,rightSmall)
 if flag:
  total = small + big
  total = total - min(small,big)
  return total
 else:
  return -1


def isBigSmallAvailable(small,big,x,a,rightBig,rightSmall):
 s = 0
 b = 0
 s = s - rightSmall
 b = b - rightBig
 length = len(a)
 for i in range(length):
  if a[i] < x:
   s += 1
  if a[i] > x:
   b += 1

  if s >= small and b >= big:
   return True
 # s = s - rightSmall
 # b = b - rightBig
 # print("big ",b)

 return False


t = int(input())
swapCount = 0
swaped = []
visited = []
for z in range(t):
 l = list(map(int,input().split()))
 n = int(l[0])
 q = int(l[1])
 aa = list(map(int,input().split()))
 for i in range(q):
  swapCount = 0
  swaped.clear()
  visited.clear()
  a = aa[:]
  # print("aa is",aa)

  x = int(input())
  # a.sort()
  xindex = a.index(x)
  opList = []
  result = binarySearch(0, n-1, xindex, opList)
  count = counter(opList, a, x)

  # print(result)
  print(count)
  # else:
  #     print(swapCount)


def binarySearch(start, end, xindex,opList):
 if start <= end:
  midindex = int((start+end)/2)

  opList.append(midindex)
  if xindex < midindex :
   return binarySearch(start, midindex-1, xindex, opList)

  elif xindex > midindex :
   return binarySearch(midindex+1, end, xindex, opList)

  else:
   return opList


def counter(opList,a, x):
 small = 0
 big = 0
 rightSmall = 0
 rightBig = 0

 for i in range(len(opList)-1):
  # print("for oplist[",i,"]=",opList[i])
  if opList[i] < opList[i+1]:
   # print("traverse to right")
   if x < a[opList[i]]:
    # print("x is small")
    small += 1
   else:
    rightSmall += 1
  else:
   # print("traverse to left")
   if x> a[opList[i]]:
    # print("x is big")
    big += 1
   else:
    rightBig += 1
 flag = isBigSmallAvailable(small,big,x,a,rightBig,rightSmall)
 if flag:
  total = small + big
  total = total - min(small,big)
  return total
 else:
  return -1


def isBigSmallAvailable(small,big,x,a,rightBig,rightSmall):
 s = 0
 b = 0
 length = len(a)
 for i in range(length):
  if a[i] < x:
   s += 1
  if a[i] > x:
   b += 1
 s = s - rightSmall
 b = b - rightBig
 # print("big ",b)
 if s>=small and b>=big:
  return True
 return False


t = int(input())
swapCount = 0
swaped = []
visited = []
for z in range(t):
 l = list(map(int,input().split()))
 n = int(l[0])
 q = int(l[1])
 aa = list(map(int,input().split()))
 for i in range(q):
  swapCount = 0
  swaped.clear()
  visited.clear()
  a = aa[:]
  # print("aa is",aa)

  x = int(input())
  # a.sort()
  xindex = a.index(x)
  opList = []
  result = binarySearch(0, n-1, xindex, opList)
  count = counter(opList, a, x)

  # print(result)
  print(count)
  # else:
  #     print(swapCount)


def binarySearch(start, end, xindex,opList):
 if start <= end:
  midindex = int((start+end)/2)

  opList.append(midindex)
  if xindex < midindex :
   return binarySearch(start, midindex-1, xindex, opList)

  elif xindex > midindex :
   return binarySearch(midindex+1, end, xindex, opList)

  else:
   return opList


def counter(opList,a, x):
 small = 0
 big = 0
 rightSmall = 0
 rightBig = 0

 for i in range(len(opList)-1):
  # print("for oplist[",i,"]=",opList[i])
  if opList[i] < opList[i+1]:
   # print("traverse to right")
   if x < a[opList[i]]:
    # print("x is small")
    small += 1
   else:
    rightSmall += 1
  else:
   # print("traverse to left")
   if x> a[opList[i]]:
    # print("x is big")
    big += 1
   else:
    rightBig += 1
 flag = isBigSmallAvailable(small,big,x,a,rightBig,rightSmall)
 if flag:
  total = small + big
  total = total - min(small,big)
  return total
 else:
  return -1


def isBigSmallAvailable(small,big,x,a,rightBig,rightSmall):
 s = 0
 b = 0
 for i in range(len(a)):
  if a[i] < x:
   s += 1
  if a[i] > x:
   b += 1
 s = s - rightSmall
 b = b - rightBig
 # print("big ",b)
 if s>=small and b>=big:
  return True
 return False


t = int(input())
swapCount = 0
swaped = []
visited = []
for z in range(t):
 l = list(map(int,input().split()))
 n = int(l[0])
 q = int(l[1])
 aa = list(map(int,input().split()))
 for i in range(q):
  swapCount = 0
  swaped.clear()
  visited.clear()
  a = aa[:]
  # print("aa is",aa)

  x = int(input())
  # a.sort()
  xindex = a.index(x)
  opList = []
  result = binarySearch(0, n-1, xindex, opList)
  count = counter(opList, a, x)

  # print(result)
  print(count)
  # else:
  #     print(swapCount)


for _ in range(int(input())):
 n,q=list(map(int,input().split()))
 l=[int(i) for i in input().split()]
 d={}
 ind={}
 for i in range(n):
  ind[l[i]]=i 
 #dup=l[:]
 dup=sorted(l)
 for i in range(n):
  chote=i 
  bade=n-i-1 
  d[dup[i]]=[chote,bade]
 for _ in range(q):
  chotewale_swap=0 
  badewale_swap=0
  x=int(input())
  #d1=d[x].copy()
  d1={}
  d1[0]=d[x][0]
  d1[1]=d[x][1]
  f=1
  low=0
  high=n-1 
  while low<=high:
   mid=(low+high)//2 
   if ind[x]==mid:
    break 
   elif ind[x]>mid and x>l[mid]:
    if d1[0]==0:
     f=-1 
     break 
    d1[0]-=1 
    low=mid+1
   elif ind[x]>mid and x<l[mid]:
    if d1[0]==0:
     f=-1 
     break 
    d1[0]-=1 
    chotewale_swap+=1
    low=mid+1
   elif ind[x]<mid and x<l[mid]:
    if d1[1]==-1:
     f=-1 
     break 
    d1[1]-=1 
    high=mid-1
   elif ind[x]<mid and x>l[mid]:
    if d1[1]==0:
     f=-1 
     break 
    d1[1]-=1 
    badewale_swap+=1 
    high=mid-1 
  if f==-1:
   print(-1)
  else:
   print(max(chotewale_swap,badewale_swap))


for _ in range(int(input())):
 n,q=list(map(int,input().split()))
 l=[int(i) for i in input().split()]
 d={}
 ind={}
 for i in range(n):
  ind[l[i]]=i 
 #dup=l[:]
 dup=sorted(l)
 for i in range(n):
  chote=i 
  bade=n-i-1 
  d[dup[i]]=[chote,bade]
 for _ in range(q):
  chotewale_swap=0 
  badewale_swap=0
  x=int(input())
  #d1=d[x].copy()
  d1={}
  d1[0]=d[x][0]
  d1[1]=d[x][1]
  f=1
  low=0
  high=n-1 
  while low<=high:
   mid=(low+high)//2 
   if ind[x]==mid:
    break 
   elif ind[x]>mid and x>l[mid]:
    d1[0]-=1 
    low=mid+1
   elif ind[x]>mid and x<l[mid]:
    if d1[0]==0:
     f=-1 
     break 
    d1[0]-=1 
    chotewale_swap+=1
    low=mid+1
   elif ind[x]<mid and x<l[mid]:
    d1[1]-=1 
    high=mid-1
   elif ind[x]<mid and x>l[mid]:
    if d1[1]==0:
     f=-1 
     break 
    d1[1]-=1 
    badewale_swap+=1 
    high=mid-1 
  if f==-1:
   print(-1)
  else:
   print(max(chotewale_swap,badewale_swap))


for _ in range(int(input())):
 n,q=list(map(int,input().split()))
 l=[int(i) for i in input().split()]
 d={}
 ind={}
 for i in range(n):
  ind[l[i]]=i 
 #dup=l[:]
 dup=sorted(l)
 for i in range(n):
  chote=i 
  bade=n-i-1 
  d[dup[i]]=[chote,bade]
 for _ in range(q):
  chotewale_swap=0 
  badewale_swap=0
  x=int(input())
  d1=d[x].copy()
  f=1
  low=0
  high=n-1 
  while low<=high:
   mid=(low+high)//2 
   if ind[x]==mid:
    break 
   elif ind[x]>mid and x>l[mid]:
    d1[0]-=1 
    low=mid+1
   elif ind[x]>mid and x<l[mid]:
    if d1[0]==0:
     f=-1 
     break 
    d1[0]-=1 
    chotewale_swap+=1
    low=mid+1
   elif ind[x]<mid and x<l[mid]:
    d1[1]-=1 
    high=mid-1
   elif ind[x]<mid and x>l[mid]:
    if d1[1]==0:
     f=-1 
     break 
    d1[1]-=1 
    badewale_swap+=1 
    high=mid-1 
  if f==-1:
   print(-1)
  else:
   print(max(chotewale_swap,badewale_swap))


def f(a,y,index,sorted_pos):
 #print(a,y,index,sorted_pos)
 n=len(a)
 low=0
 high=n-1
 L,R=0,0
 l,r=0,0
 while(low<=high):
  mid=(low+high)//2
  #print(low,high,mid)
  if(a[mid]== y):
   break
  elif(mid > index[y]):
   high=mid-1
   L+=1
   #print("L")
   if(a[mid] <y):
    l+=1
    #print(" l ")
  else:
   low=mid+1
   R+=1
   #print("R")
   if(a[mid]>y):
    r+=1
    #print("r")
 x=sorted_pos[y]
 #print(L,R,l,r,x,n-x-1)
 if(R>x or L> n-x-1):
  print("-1")
 else:
  print(max(l,r))


def fun():
 test=int(input())
 for t in range(test):
  n,q=list(map(int,input().split()))
  arr=list(map(int,input().split()))
  index= dict()
  for i in range(n):
   index[arr[i]]=i
  sorted_pos=dict()
  a=sorted(arr)
  for i in range(n):
   sorted_pos[a[i]]=i
  for x in range(q):
   y=int(input())
   f(arr,y,index,sorted_pos)

fun()



for _ in range(int(input())):
 n,q=list(map(int,input().split()))
 l=[int(i) for i in input().split()]
 d={}
 ind={}
 for i in range(n):
  ind[l[i]]=i 
 dup=l[:]
 dup=sorted(dup)
 for i in range(n):
  chote=i 
  bade=n-i-1 
  d[dup[i]]=[chote,bade]
 for _ in range(q):
  chotewale_swap=0 
  badewale_swap=0
  x=int(input())
  d1=d[x].copy()
  f=1
  low=0
  high=n-1 
  while low<=high:
   mid=(low+high)//2 
   if ind[x]==mid:
    break 
   elif ind[x]>mid and x>l[mid]:
    d1[0]-=1 
    low=mid+1
   elif ind[x]>mid and x<l[mid]:
    if d1[0]==0:
     f=-1 
     break 
    d1[0]-=1 
    chotewale_swap+=1
    low=mid+1
   elif ind[x]<mid and x<l[mid]:
    d1[1]-=1 
    high=mid-1
   elif ind[x]<mid and x>l[mid]:
    if d1[1]==0:
     f=-1 
     break 
    d1[1]-=1 
    badewale_swap+=1 
    high=mid-1 
  if f==-1:
   print(-1)
  else:
   print(max(chotewale_swap,badewale_swap))


for _ in range(int(input())):
 n,q=list(map(int,input().split()))
 l=[int(i) for i in input().split()]
 d={}
 ind={}
 for i in range(n):
  ind[l[i]]=i 
 dup=l.copy()
 dup.sort()
 for i in range(n):
  chote=i 
  bade=n-i-1 
  d[dup[i]]=[chote,bade]
 for _ in range(q):
  chotewale_swap=0 
  badewale_swap=0
  x=int(input())
  d1=d[x].copy()
  f=1
  low=0
  high=n-1 
  while low<high:
   mid=(low+high)//2 
   if ind[x]==mid:
    break 
   elif ind[x]>mid and x>l[mid]:
    d1[0]-=1 
    low=mid+1
   elif ind[x]>mid and x<l[mid]:
    if d1[0]==0:
     f=-1 
     break 
    d1[0]-=1 
    chotewale_swap+=1
    low=mid+1
   elif ind[x]<mid and x<l[mid]:
    d1[1]-=1 
    high=mid-1
   elif ind[x]<mid and x>l[mid]:
    if d1[1]==0:
     f=-1 
     break 
    d1[1]-=1 
    badewale_swap+=1 
    high=mid-1 
  if f==-1:
   print(-1)
  else:
   print(max(chotewale_swap,badewale_swap))


for _ in range(int(input())):
 n,q=list(map(int,input().split()))
 l=[int(i) for i in input().split()]
 d={}
 ind={}
 for i in range(n):
  ind[l[i]]=i 
 dup=l.copy()
 dup.sort()
 for i in range(n):
  chote=i 
  bade=n-i-1 
  d[dup[i]]=[chote,bade]
 for _ in range(q):
  chotewale_swap=0 
  badewale_swap=0
  x=int(input())
  d1=d[x].copy()
  f=1
  low=0
  high=n-1 
  while low<=high:
   mid=(low+high)//2 
   if ind[x]==mid:
    break 
   elif ind[x]>mid and x>l[mid]:
    d1[0]-=1 
    low=mid+1
   elif ind[x]>mid and x<l[mid]:
    if d1[0]==0:
     f=-1 
     break 
    d1[0]-=1 
    chotewale_swap+=1
    low=mid+1
   elif ind[x]<mid and x<l[mid]:
    d1[1]-=1 
    high=mid-1
   elif ind[x]<mid and x>l[mid]:
    if d1[1]==0:
     f=-1 
     break 
    d1[1]-=1 
    badewale_swap+=1 
    high=mid-1 
  if f==-1:
   print(-1)
  else:
   print(max(chotewale_swap,badewale_swap))


t = int(input())
while t > 0:
 t -= 1
 n, q = list(map(int, input().split()))
 arr = list(map(int, input().split()))
 tarr = sorted(arr)
 d = {}
 inddir = {}
 for i in range(len(arr)):
  inddir[arr[i]] = i
 tarrlen = len(tarr)
 for i in range(len(tarr)):
  d[tarr[i]] = [i, tarrlen - i - 1]
 for _ in range(q):
  k = int(input())
  find = inddir[k]
  l, r = 1, tarrlen
  gs=0
  ls=0
  sw = 0
  tempc = d[k].copy()
  while l <= r:
   mid = (l + r) // 2
   if mid - 1 == find:
    break
   elif mid - 1 < find:
    if arr[mid - 1] < k:
     if tempc[0] > 0:
      tempc[0] -= 1
     else:
      sw = -1
      break
    else:
     if tempc[0] > 0:
      tempc[0] -= 1
      sw += 1
      ls+=1
     else:
      sw = -1
      break
    l = mid + 1
   else:
    if arr[mid - 1] > k:
     if tempc[1] > 0:
      tempc[1] -= 1
     else:
      sw = -1
      break
    else:
     if tempc[1] > 0:
      tempc[1] -= 1
      sw += 1
      gs+=1
     else:
      sw = -1
      break
    r = mid - 1
  print(max(ls,gs) if sw!=-1 else -1)


T=int(input())
for xs in range(T):
 N,Q=[int(o) for o in input().split()]
 a=[int(o) for o in input().split()]
 b=sorted(a)
 Di={} #Dictionary
 for i in range(len(a)):
  Di[a[i]]=[i]
 for i in range(len(b)):
  Di[b[i]].append(i) 
 r=[]
 qu=[int(input()) for j in range(Q)]
 for q in qu:
  low=1
  high=N
  ind=Di[q][0]+1
  b=[]
  f=[]
  s=0
  maxs=N-Di[q][1]-1
  mins=N-maxs-1
  while low<=high:   #Binary Search algorithm
   mid=(low+high)//2
   if mid==ind:
    break
   elif mid<ind:
    low=mid+1
    if a[mid-1]>q:
     b.append(a[mid-1])
    else:
     mins-=1 
   elif mid>ind:
    high=mid-1
    if a[mid-1]<q:
     f.append(a[mid-1])
    else:
     maxs-=1
  lenf=len(f)
  lenb=len(b)
  sw=min(lenf,lenb)
  rs=max(lenf,lenb)-sw
  mins,maxs=mins-sw,maxs-sw
  ex=min(mins,maxs)
  if ex<rs: 
   r.append(-1)
   s=1
  else:
   sw+=rs # When ex>=rs
  if not s:
   r.append(sw) 
 for ans in r:
  print(ans) #Final