# Triple Sort of May long challenge -Some subtasks are showing wrong results

def swap(a, b, c):
k = a[b]
a[b] = a[c]
a[c] = k
def oddeven(n):
if n%2 == 0:
return 1
else:
return -1

T=int(input())
for _ in range(T):
n, k = map(int, input().split())
a = list(map(int, input().split()))
odd = []
even = []
for i in range(n):
beta = []
while a[i]!=i+1:
beta.append([i+1,a[i]])
swap(a, i, a[i]-1)
if beta == []:
continue
elif oddeven(len(beta)) == 1:
even.append(beta)
else:
odd.append(beta)

if oddeven(len(odd)) == -1:
print(-1)
else:
print((n - len(even)) // 2)
for i1 in range(len(even)):
for i2 in range(len(even[i1])//2):
print(even[i1][2*i2][0], even[i1][2*i2][1], even[i1][2*i2+1][1] )
for i1 in range(len(odd)//2):
for i2 in range((len(odd[2*i1])-1)//2):
print(odd[2*i1][2 * i2][0], odd[2*i1][2 * i2][1], odd[2*i1][2 * i2 + 1][1])
print(odd[2*i1][-1][0], odd[2*i1][-1][1], odd[2*i1+1][0][1])
print(odd[2*i1+1][0][0], odd[2*i1+1][0][1], odd[2*i1][-1][0])
for i3 in range(1, (len(odd[2*i1+1])+1)//2):
print(odd[2*i1+1][2 * i3-1][0], odd[2*i1+1][2 * i3-1][1], odd[2*i1+1][2 * i3 ][1])