Given a positive integer n and an integer k such that 0 ≤ k ≤ n, find any permutation A of 1, 2 \dots n such that the number of indices for which A_{i}=i is exactly k. If there exists no such permutation, print -1. If there exist multiple such permutations, print any one of them.

------ Input Format ------ 

- First line of the input contains T, the number of test cases. Then the test cases follow.
- Each test case contains a single line of input, two integers n, k.

------ Output Format ------ 

For each test case, print a permutation in a single line, if a permutation with the given constraints exists. Print -1 otherwise.

------ Constraints ------ 

$1 ≤ T ≤ 10^{5}$
$1 ≤ n ≤ 10^{5}$
$0 ≤ k ≤ n$
- Sum of $n$ over all test cases doesn't exceed $2 \cdot 10^{6}$

----- Sample Input 1 ------ 
3
2 1
3 1
4 2

----- Sample Output 1 ------ 
-1
1 3 2
3 2 1 4

----- explanation 1 ------ 
Test case $1$: There are total $2$ permutations of $[1, 2]$  which are $[1, 2]$ and $[2, 1]$. There are $2$ indices in $[1, 2]$ and $0$ indices in $[2, 1]$ for which $A_{i}=i$ holds true. Thus, there doesn't exist any permutation of $[1, 2]$ with exactly $1$ index $i$ for which $A_{i}=i$ holds true.

Test case $2$: Consider the permutation $A = [1, 3, 2]$. We have $A_{1} = 1$, $A_{2} = 3$ and $A_{3} = 2$. So, this permutation has exactly $1$ index such that $A_{i}=i$.

Difficulty: MEDIUM
Tags: Constructive algorithms

Ad-hoc

Starter code:

Solutions:
for _ in range(int(input())):
	(n, k) = map(int, input().split())
	if n - k == 1:
		print(-1)
		continue
	for i in range(1, k + 1):
		print(i, end=' ')
	if (n - k) % 2 == 1:
		for i in range(k + 2, n + 1):
			print(i, end=' ')
		print(k + 1)
	else:
		for i in range(n, k, -1):
			print(i, end=' ')
		print()


t = int(input())
for _ in range(t):
	(n, k) = map(int, input().split())
	l = []
	for i in range(1, k + 1):
		l.append(i)
	if n - k == 1:
		print(-1)
	elif n == k:
		print(*l)
	else:
		l.append(n)
		for j in range(k + 1, n):
			l.append(j)
		print(*l)


for i in range(int(input())):
	(n, k) = map(int, input().split())
	l = list(range(1, n + 1))
	if k == n - 1:
		print(-1)
	elif k == n:
		print(*l)
	else:
		l1 = l[:k] + [l[-1]] + l[k:-1]
		print(*l1)


try:
	t = int(input())
	while t:
		(n, k) = map(int, input().split())
		if n - k == 1:
			print('-1')
		elif n == k:
			for i in range(1, n + 1):
				print(i, end=' ')
		else:
			for i in range(1, k + 1):
				print(i, end=' ')
			for i in range(k + 2, n + 1):
				print(i, end=' ')
			print(k + 1)
		t -= 1
except EOFError as e:
	print(e)


import os, sys, io, math
from array import array
from math import *
I = lambda : [*map(int, sys.stdin.readline().split())]
IS = lambda : input()
IN = lambda : int(input())
IF = lambda : float(input())
for _ in range(IN()):
	(n, k) = map(int, input().split())
	if n - k == 1:
		print(-1)
	else:
		for i in range(1, k + 1):
			print(i, end=' ')
		if n > k:
			print(n, end=' ')
		for i in range(k + 1, n):
			print(i, end=' ')
		print()


def soln(n, k):
	if k == n - 1:
		return [-1]
	if k == n:
		return [i for i in range(1, n + 1)]
	if n - k & 1:
		res = [i for i in range(1, k + 1)]
		res += [i for i in range(n, k, -1)]
		match = (k + n - 1) // 2
		(res[match], res[-1]) = (res[-1], res[match])
	else:
		res = [i for i in range(1, k + 1)]
		res += [i for i in range(n, k, -1)]
	return res
for _ in range(int(input())):
	(n, k) = map(int, input().split())
	print(*soln(n, k))


tests = int(input())
for i in range(tests):
	(n, k) = map(int, input().split())
	if k == n - 1:
		print(-1)
	else:
		ans = [i + 1 for i in range(k)]
		if k < n:
			ans.append(n)
		for i in range(k + 1, n):
			ans.append(i)
		print(*ans)


for _ in range(int(input())):
	(n, k) = map(int, input().split())
	if k == n - 1:
		print(-1)
		continue
	if k == n:
		print(*range(1, k + 1))
		continue
	print(*range(1, k + 1), n, *range(k + 1, n))


t = int(input())
for _ in range(t):
	(n, k) = map(int, input().split())
	if n - k == 1:
		print(-1)
	elif n == k:
		for i in range(n):
			print(i + 1, end=' ')
	else:
		l = list(range(1, k + 1))
		ele = [k + 1]
		l1 = list(range(k + 2, n + 1))
		print(*l + l1 + ele)


t = int(input())
for i in range(t):
	(n, k) = map(int, input().split())
	if k == n - 1:
		print(-1)
	else:
		for j in range(n - k):
			print((j + 1) % (n - k) + 1, end=' ')
		for z in range(n - k, n):
			print(z + 1, end=' ')
		print()


t = int(input())
while t:
	(n, k) = map(int, input().split())
	if k == n - 1:
		print(-1)
	else:
		for i in range(n - k):
			print((i + 1) % (n - k) + 1, end=' ')
		for i in range(n - k, n):
			print(i + 1, end=' ')
		print()
	t -= 1


t = int(input())
for tc in range(t):
	(n, k) = map(int, input().split())
	ans = []
	if n == k:
		for i in range(1, n + 1):
			ans.append(i)
	elif k == n - 1:
		ans.append(-1)
	else:
		for i in range(1, k + 1):
			ans.append(i)
		for i in range(k + 2, n + 1):
			ans.append(i)
		ans.append(k + 1)
	print(*ans)


for i in range(int(input())):
	(n, k) = map(int, input().split())
	if k == n - 1:
		print(-1)
	elif n == 1:
		if k == 1:
			print(1)
		else:
			print(-1)
	elif n == 2:
		if k == 1:
			print(-1)
		elif k == 0:
			print(2, 1)
		else:
			print(1, 2)
	else:
		A = []
		for j in range(n):
			A.append(j + 1)
		if n == k:
			print(*A)
		elif k != 0:
			for j in range(k, n - 1):
				l = A[j]
				A[j] = A[j + 1]
				A[j + 1] = l
			print(*A)
		else:
			for j in range(n - 1):
				l = A[j]
				A[j] = A[j + 1]
				A[j + 1] = l
			print(*A)


t = int(input())
for _ in range(0, t):
	inputs = [int(num) for num in input().split()]
	n = inputs[0]
	k = inputs[1]
	not_in_pos = n - k
	if not_in_pos == 1:
		print(-1)
	elif k != n:
		s = ''
		for i in range(0, k):
			s += str(i + 1) + ' '
		i = k + 2
		for j in range(0, not_in_pos - 1):
			s += str(i) + ' '
			i += 1
		s += str(k + 1)
		print(s)
	else:
		s = ''
		for i in range(0, n):
			s += str(i + 1) + ' '
		print(s)


tc = int(input())
while tc != 0:
	tc -= 1
	(n, k) = map(int, input().split())
	if n - k == 1:
		print('-1')
		continue
	arr = [0] * n
	j = 0
	for i in range(k):
		arr[j] = i + 1
		j += 1
	for i in range(n, k, -1):
		arr[j] = i
		j += 1
	if (n - k) % 2 == 1:
		hf = (n + k + 1) // 2 - 1
		(arr[hf], arr[hf + 1]) = (arr[hf + 1], arr[hf])
	print(*arr)


a = int(input())
while a:
	(n, k) = map(int, input().split())
	if n - k == 1:
		print('-1')
	elif n == k:
		for i in range(1, n + 1):
			print(i, end=' ')
	else:
		for i in range(1, k + 1):
			print(i, end=' ')
		for i in range(k + 2, n + 1):
			print(i, end=' ')
		print(k + 1)
	a -= 1


for _ in range(int(input())):
	(n, k) = map(int, input().split())
	lt = []
	if k == n - 1:
		print(-1)
	elif k == n:
		lt = list(range(1, k + 1))
		print(*lt)
	else:
		for i in range(1, k + 1):
			lt.append(i)
		for i in range(k + 2, n + 1):
			lt.append(i)
		lt.append(k + 1)
		print(*lt)


t = int(input())
for i in range(t):
	(n, k) = map(int, input().split())
	if k == n - 1:
		print('-1')
	elif k == n:
		ans = list(range(1, n + 1))
		print(*ans)
	else:
		ans = list(range(1, n + 1))
		ans = ans[:k] + ans[k + 1:n] + [ans[k]]
		print(*ans)


t = int(input())
for i in range(t):
	(a, b) = map(int, input().split())
	if a - b == 1:
		print(-1)
		continue
	temp = [i for i in range(1, a + 1)]
	temp1 = temp[0:b]
	temp2 = temp[b:][::-1]
	if (a - b) % 2 != 0:
		dummy = len(temp2) // 2
		(temp2[dummy], temp2[dummy + 1]) = (temp2[dummy + 1], temp2[dummy])
	s = ''
	for i in temp1:
		s += str(i) + ' '
	for i in temp2:
		s += str(i) + ' '
	s = s[0:len(s) - 1]
	print(s)


for _ in range(int(input())):
	(n, k) = map(int, input().split(' '))
	arr = [i for i in range(1, n + 1)]
	if k == n:
		print(*arr)
	elif n == 0 or k == n - 1:
		print(-1)
	else:
		arr2 = arr[:k]
		arr3 = arr[k:]
		arr3.insert(0, arr3[len(arr3) - 1])
		arr3.pop(len(arr3) - 1)
		print(*arr2 + arr3)


for _ in range(int(input())):
	(n, k) = map(int, input().split())
	s = [0] * n
	if n - k == 1:
		print(-1)
	elif n > k:
		for i in range(k):
			s[i] = i + 1
		for i in range(k, n - 1):
			s[i] = i + 2
		s[n - 1] = k + 1
		print(*s)
	else:
		for i in range(n):
			s[i] = i + 1
		print(*s)


t = int(input())
while t > 0:
	(n, k) = map(int, input().split())
	if k == n - 1:
		print(-1)
	elif n == k:
		for i in range(1, n + 1):
			print(i, end=' ')
		print()
	else:
		li = [0] * n
		c = 1
		for i in range(0, k):
			li[i] = c
			c = c + 1
		c = k + 2
		for i in range(k, n):
			li[i] = c
			c = c + 1
		li[n - 1] = k + 1
		for i in range(0, n):
			print(li[i], end=' ')
		print()
	t = t - 1


from typing import DefaultDict
from math import sqrt
import math

def calculate(p, q):
	mod = 998244353
	expo = 0
	expo = mod - 2
	while expo:
		if expo & 1:
			p = p * q % mod
		q = q * q % mod
		expo >>= 1
	return p

def prime(n):
	prime_flag = 0
	if n > 1:
		for i in range(2, int(sqrt(n)) + 1):
			if n % i == 0:
				prime_flag = 1
				break
		if prime_flag == 0:
			return True
		else:
			return False
	else:
		return True

def smallestDivisor(n):
	if n % 2 == 0:
		return 2
	i = 3
	while i * i <= n:
		if n % i == 0:
			return i
		i += 2
	return n
for _ in range(int(input())):
	bool = True
	flag = True
	ans = 1
	(n, k) = map(int, input().split())
	c = 2
	if k == n - 1:
		print(-1)
	else:
		for _ in range(n - k - 1):
			print(c, end=' ')
			c += 1
		print(1, end=' ')
		for i in range(c, n + 1):
			print(i, end=' ')
		print()


t = int(input())
while t:
	(n, k) = map(int, input().split())
	if n - k == 1:
		print('-1')
	elif n == k:
		for l in range(1, n + 1):
			print(l, end=' ')
	else:
		for l in range(1, k + 1):
			print(l, end=' ')
		for l in range(k + 2, n + 1):
			print(l, end=' ')
		print(k + 1)
	t -= 1


t = int(input())
while t > 0:
	(a, b) = map(int, input().split())
	l = []
	l1 = []
	for i in range(1, b + 1, 1):
		l.append(i)
	for i in range(b + 1, a + 1, 1):
		l1.append(i)
	if abs(a - b) == 1:
		print(-1)
	else:
		if len(l1) % 2 == 0:
			l1.reverse()
		else:
			l1.reverse()
			j = len(l1)
			k = len(l1) // 2
			x = l1[k]
			y = l1[len(l1) - 1]
			l1.pop(k)
			l1.pop(len(l1) - 1)
			l1.insert(j - 1, x)
			l1.insert(k, y)
		for i in l:
			print(i, end=' ')
		for i in l1:
			print(i, end=' ')
		print()
	t -= 1


t = int(input())
for _ in range(t):
	(n, k) = map(int, input().split())
	if k == n - 1:
		print(-1)
	elif n == k:
		for i in range(1, n + 1):
			print(i, end=' ')
	else:
		arr = [0] * n
		for i in range(1, k + 1):
			arr[i - 1] = i
		arr[k] = n
		for i in range(k + 1, n):
			arr[i] = i
		print(*arr)


t = int(input())
for i in range(t):
	(n, k) = map(int, input().split())
	if n - k == 1:
		print(-1)
		continue
	for i in range(1, k + 1):
		print(i, end=' ')
	for i in range(k + 1, n):
		print(i + 1, end=' ')
	if k != n:
		print(k + 1, end=' ')
	print()


import sys, math, io, os, time, itertools, collections
mod = 10 ** 9 + 7
sys.setrecursionlimit(10000)
i = sys.stdin.readline
global start, end

def main():
	(n, K) = [int(k) for k in i().split()]
	if K + 1 == n:
		print('-1')
	elif K == n:
		print(*[x for x in range(1, n + 1)])
	else:
		l = [x for x in range(1, K + 1)]
		l.append(n)
		l += [x for x in range(K + 1, n)]
		print(*l)
t = 1
t = int(i())
start = time.perf_counter()
for _ in range(t):
	main()
end = time.perf_counter()


t = int(input())
for tc in range(t):
	(n, k) = map(int, input().split())
	if k == n - 1:
		print(-1)
		continue
	s1 = []
	s2 = []
	for i in range(1, k + 1):
		s1.append(i)
	for i in range(k + 2, n + 1):
		s2.append(i)
	if k != n:
		res = s1 + s2 + [k + 1]
		print(*res)
	else:
		res = s1 + s2
		print(*res)


def str_to_int_arr(arr):
	return [int(i) for i in arr]
t = int(input())
for i in range(t):
	[n, k] = input().split()
	out = []
	rev = []
	if int(k) == int(n) - 1:
		print('-1')
		continue
	for i in range(int(n)):
		if i < int(k):
			out.append(str(i + 1))
		else:
			rev.append(str(i + 1))
	out = out + rev[1:] + rev[0:1]
	print(' '.join(out))


t = int(input())
while t != 0:
	(n, k) = [int(x) for x in input().split()]
	arr = []
	flag = False
	if n == k + 1:
		print(-1)
	else:
		for i in range(1, k + 1):
			arr.append(i)
		for j in range(k + 2, n + 1):
			arr.append(j)
			flag = True
		if flag == True:
			arr.append(k + 1)
		print(*arr)
	t -= 1


n = int(input())
for i in range(n):
	a = input().split()
	n = int(a[0])
	k = int(a[1])
	if n - k == 0:
		for j in range(k):
			print(j + 1, end=' ')
	elif n - k == 1:
		print(-1)
	else:
		for j in range(k):
			print(j + 1, end=' ')
		print(n, end=' ')
		for j in range(k + 1, n):
			print(j, end=' ')
		print()


tv = int(input())
ans = []
while tv > 0:
	an = list(map(int, input().split(' ')))
	x = an[0] - an[1]
	if x == 1:
		ans.append(-1)
	else:
		anss = ''
		n = an[0]
		t = 1
		a = 1
		wr = 0
		while t <= an[1]:
			anss += str(t) + ' '
			t += 1
			a += 1
		while n >= t:
			if a == n:
				wr = a
				anss += str(n - 1) + ' ' + str(n) + ' '
				n -= 2
			else:
				anss += str(n) + ' '
				a += 1
				n -= 1
		ans.append(anss)
	tv -= 1
y = 0
while y < len(ans):
	print(ans[y])
	y += 1


t = int(input())
for i in range(t):
	tmp = input().split(' ')
	n = int(tmp[0])
	k = int(tmp[1])
	if n - k == 1:
		print(-1)
	else:
		for j in range(k):
			print(j + 1, end=' ')
		if k != n:
			for l in range(k + 1, n):
				print(l + 1, end=' ')
			print(k + 1)
		else:
			print()


for _ in range(int(input())):
	(n, k) = [int(i) for i in input().split()]
	x = list(range(1, n + 1))
	if n == k:
		for i in range(n - 1):
			print(x[i], end=' ')
		print(x[n - 1])
	elif n - 1 == k:
		print(-1)
	else:
		for i in range(n - k - 1):
			(x[i], x[i + 1]) = (x[i + 1], x[i])
		for i in range(n - 1):
			print(x[i], end=' ')
		print(x[n - 1])


for _ in range(int(input())):
	(n, k) = map(int, input().split())
	if k + 1 == n:
		ans = -1
	else:
		arr = list(range(1, n + 1))
		t1 = arr[:k]
		t2 = sorted(arr[k:], reverse=True)
		test = []
		ans = t1 + t2
		for i in range(k, n):
			if ans[i] == i + 1:
				(ans[i], ans[i + 1]) = (ans[i + 1], ans[i])
	print(*ans) if type(ans) != int else print(ans)


from math import gcd, log, ceil, floor
import random as rd
for _ in range(int(input())):
	(n, k) = map(int, input().split())
	if k + 1 == n:
		ans = -1
	else:
		arr = list(range(1, n + 1))
		t1 = arr[:k]
		t2 = sorted(arr[k:], reverse=True)
		test = []
		ans = t1 + t2
		for i in range(k, n):
			if ans[i] == i + 1:
				(ans[i], ans[i + 1]) = (ans[i + 1], ans[i])
	if type(ans) != int:
		print(*ans)
	else:
		print(ans)


r = int(input())
for _ in range(r):
	(n, k) = map(int, input().split())
	l = list(range(1, k + 1))

	def rot(ar):
		return ar[1:] + ar[:1]
	ar = list(range(k + 1, n + 1))
	res = l + rot(ar)
	if k == n - 1:
		print(-1)
	else:
		print(' '.join(map(str, res)))


def mapIn():
	return map(int, input().split())

def listIn():
	return list(map(int, input().split()))
intMax = float('inf')
intMin = float('-inf')
d4x = [1, -1, 0, 0]
d4y = [0, 0, 1, -1]
d8x = [1, 1, -1, -1, 0, 0, 0, 0]
d8y = [0, 1, 0, 0]

def solve():
	(n, k) = mapIn()
	if k == n - 1:
		print(-1)
		return
	for i in range(1, k + 1):
		print(i, end=' ')
	for i in range(k + 2, n + 1):
		print(i, end=' ')
	if k != n:
		print(k + 1)
	else:
		print()
t = int(input())
for _ in range(t):
	solve()


for _ in range(int(input())):
	(n, y) = map(int, input().split())
	if n == y + 1:
		print(-1)
	elif n == y:
		for i in range(1, n + 1):
			print(i, end=' ')
		print()
	else:
		for i in range(1, y + 1):
			print(i, end=' ')
		x = y + 2
		while x <= n:
			print(x, end=' ')
			x += 1
		print(y + 1)


def permutate(n, k):
	if k == n - 1:
		print(-1)
	else:
		z = []
		for i in range(1, k + 1):
			z.append(i)
		k += 2
		for j in range(k, n + 1):
			z.append(j)
		if k - 2 == n:
			pass
		else:
			z.append(k - 1)
		for i in z:
			print(i, end=' ')
		print()
x = int(input())
for i in range(x):
	z = [int(x) for x in input().split()]
	(n, k) = (z[0], z[1])
	permutate(n, k)


t = int(input())
while t:
	t -= 1
	(n, k) = map(int, input().split())
	l = []

	def swap(a, b):
		temp = a
		a = b
		b = temp
	if n - k == 1:
		print(-1)
	else:
		for i in range(1, n + 1):
			l.append(i)
		for i in range(k, n - 1):
			temp = l[i]
			l[i] = l[i + 1]
			l[i + 1] = temp
		print(*l)


import sys
f = sys.stdin
next(f)
for s in f:
	(n, k) = map(int, s.split())
	a = range(1, n + 1)
	n -= k
	if n == 1:
		print(-1)
	else:
		print(*a[1:n], *a[:n > 0], *a[n:])


T = int(input())
for i in range(T):
	(n, k) = map(int, input().split())
	if k == n - 1:
		print(-1)
	else:
		for j in range(1, k + 1):
			print(j, end=' ')
		for j in range(k + 1, n + 1):
			if j == n:
				print(k + 1)
			else:
				print(j + 1, end=' ')


T = int(input())
for i in range(T):
	(n, k) = [int(i) for i in input().split()]
	F = 0
	x = 0
	if k == n - 1:
		print(-1)
	elif k == n:
		for i in range(1, k + 1):
			print(i, end=' ')
		print()
	else:
		for i in range(1, k + 1):
			print(i, end=' ')
		for i in range(k + 2, n + 1):
			print(i, end=' ')
		print(k + 1)