Chef received a permutation $P_1, P_2, \ldots, P_N$ and also an integer $D$ from his good friend Grux, because Grux was afraid he would forget them somewhere. However, since Grux was just playing with the permutation, it was all shuffled, and Chef only likes sorted permutations, so he decided to sort it by performing some swaps.
Chef wants to use the integer $D$ he just received, so he is only willing to swap two elements of the permutation whenever their absolute difference is exactly $D$. He has limited time, so you should determine the minimum number of swaps he needs to perform to sort the permutation, or tell him that it is impossible to sort it his way.
-----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 $D$.
- The second line contains $N$ space-separated integers $P_1, P_2, \ldots, P_N$.
-----Output-----
For each test case, print a single line containing one integer ― the minimum number of swaps, or $-1$ if it is impossible to sort the permutation.
-----Constraints-----
- $1 \le T \le 20$
- $1 \le N \le 200,000$
- $1 \le D \le N$
- $1 \le P_i \le N$ for each valid $i$
- $P_1, P_2, \ldots, P_N$ are pairwise distinct
- the sum of $N$ over all test cases does not exceed $10^6$
-----Subtasks-----
Subtask #1 (20 points): $D = 1$
Subtask #2 (30 points):
- $N \le 1,000$
- the sum of $N$ over all test cases does not exceed $10,000$
Subtask #3 (50 points): original constraints
-----Example Input-----
2
5 2   
3 4 5 2 1
5 2    
4 3 2 1 5  
-----Example Output-----
3
-1
-----Explanation-----
Example case 1: Chef can perform the following swaps in this order:
- swap the first and fifth element
- swap the third and fifth element
- swap the second and fourth element
# Python3 program to count inversions using 
# Binary Indexed Tree 

# Returns sum of arr[0..index]. This function 
# assumes that the array is preprocessed and 
# partial sums of array elements are stored 
# in BITree[]. 
def getSum( BITree, index): 
 sum = 0 # Initialize result 
 
 # Traverse ancestors of BITree[index] 
 while (index > 0): 

  # Add current element of BITree to sum 
  sum += BITree[index] 

  # Move index to parent node in getSum View 
  index -= index & (-index) 

 return sum

# Updates a node in Binary Index Tree (BITree) 
# at given index in BITree. The given value 
# 'val' is added to BITree[i] and all of its 
# ancestors in tree. 
def updateBIT(BITree, n, index, val): 

 # Traverse all ancestors and add 'val' 
 while (index <= n): 

  # Add 'val' to current node of BI Tree 
  BITree[index] += val 

  # Update index to that of parent 
  # in update View 
  index += index & (-index) 

# Returns count of inversions of size three 
def getInvCount(arr, n): 

 invcount = 0 # Initialize result 

 # Find maximum element in arrays 
 maxElement = max(arr) 

 # Create a BIT with size equal to 
 # maxElement+1 (Extra one is used 
 # so that elements can be directly 
 # be used as index) 
 BIT = [0] * (maxElement + 1) 
 for i in range(1, maxElement + 1): 
  BIT[i] = 0
 for i in range(n - 1, -1, -1): 

  invcount += getSum(BIT, arr[i] - 1) 
  updateBIT(BIT, maxElement, arr[i], 1) 
 return invcount 
 

 
# This code is contributed by 
# Shubham Singh(SHUBHAMSINGH10) 

for _ in range(int(input())):
 n,k=map(int,input().split())
 l=[int(i) for i in input().split()]
 if k==1:
  print(getInvCount(l,n))
  continue 
 mat=l[:]
 f=1 
 sm=0
 from collections import Counter
 for x in range(k):
  mat=[]
  li=[]
  le=0 
  for y in range(x,n,k):
   mat.append(l[y])
   li.append(y+1)
   le+=1 
  # print(mat,li)
  if Counter(mat)!=Counter(li):
   f=0
   break 
  else:
   li=[]
   le=len(mat)
   for i in range(le):
    curr=[mat[i],i]
    li.append(curr)
   li.sort() 
   vis=[0]*le 
   for i in range(le):
    if vis[i] or li[i][1]==i:
     continue 
    c=0 
    j=i 
    while not vis[j]:
     vis[j]=1 
     j=li[j][1]
     c+=1 
    if c:
     sm+=(c-1)
    
 if f==0:
  print(-1)
 else: 
  print(sm)
# Python3 program to count inversions using 
# Binary Indexed Tree 

# Returns sum of arr[0..index]. This function 
# assumes that the array is preprocessed and 
# partial sums of array elements are stored 
# in BITree[]. 
def getSum( BITree, index): 
 sum = 0 # Initialize result 
 
 # Traverse ancestors of BITree[index] 
 while (index > 0): 

  # Add current element of BITree to sum 
  sum += BITree[index] 

  # Move index to parent node in getSum View 
  index -= index & (-index) 

 return sum

# Updates a node in Binary Index Tree (BITree) 
# at given index in BITree. The given value 
# 'val' is added to BITree[i] and all of its 
# ancestors in tree. 
def updateBIT(BITree, n, index, val): 

 # Traverse all ancestors and add 'val' 
 while (index <= n): 

  # Add 'val' to current node of BI Tree 
  BITree[index] += val 

  # Update index to that of parent 
  # in update View 
  index += index & (-index) 

# Returns count of inversions of size three 
def getInvCount(arr, n): 

 invcount = 0 # Initialize result 

 # Find maximum element in arrays 
 maxElement = max(arr) 

 # Create a BIT with size equal to 
 # maxElement+1 (Extra one is used 
 # so that elements can be directly 
 # be used as index) 
 BIT = [0] * (maxElement + 1) 
 for i in range(1, maxElement + 1): 
  BIT[i] = 0
 for i in range(n - 1, -1, -1): 

  invcount += getSum(BIT, arr[i] - 1) 
  updateBIT(BIT, maxElement, arr[i], 1) 
 return invcount 
 

 
# This code is contributed by 
# Shubham Singh(SHUBHAMSINGH10) 

for _ in range(int(input())):
 n,k=map(int,input().split())
 l=[int(i) for i in input().split()]
 if k==1:
  print(getInvCount(l,n))
  continue 
 mat=l[:]
 f=1 
 sm=0
 from collections import Counter
 for x in range(k):
  mat=[]
  li=[]
  le=0 
  for y in range(x,n,k):
   mat.append(l[y])
   li.append(y+1)
   le+=1 
  # print(mat,li)
  if Counter(mat)!=Counter(li):
   f=0
   break 
  else:
   li=[]
   le=len(mat)
   for i in range(le):
    curr=[mat[i],i]
    li.append(curr)
   li.sort() 
   vis=[0]*le 
   for i in range(le):
    if vis[i] or li[i][1]==i:
     continue 
    c=0 
    j=i 
    while not vis[j]:
     vis[j]=1 
     j=li[j][1]
     c+=1 
    if c:
     sm+=(c-1)
    
 if f==0:
  print(-1)
 else: 
  print(sm)