Blobo2 is in his practical exam. The teacher gave him a *permutation* A of N integers.
 
The teacher has allowed Blobo2 to make a certain type of operation on the *permutation*. In one operation, he can:
Apply left shift on the permutation. In other words, he can take the first element of the permutation and move it to the back of the permutation. 

The teacher has asked Blobo2 to find the lexicographically smallest permutation possible after applying any (possibly zero) number of given operations.

Since Blobo2 wants to impress his teacher, he decided to perform at most two swaps in addition to the allowed operation.

Find the lexicographically smallest possible permutation Blobo2 can generate after applying at most two swaps and any number of given operations.

Note:
A permutation of size N consists of all integers from 1 to N exactly once.
During a swap, Blobo2 can choose any two indices i and j (1≤ i,j≤ N) and swap A_{i} with A
_j.
A permutation X is said to be lexicographically smaller than a permutation Y if X_{i} < Y_{i}, where i is the first index where both the permutations differ.

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

- The first line of input will contain a single integer T, denoting the number of test cases.
- The second line will contain the integer N, denoting the size of the permutation.
- The third line contains N distinct integers, the elements of the permutation A.

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

Output the lexicographically smallest possible permutation Blobo2 can generate after applying at most two swaps and any number of given operations.

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

$1≤ T≤ 5\cdot 10^{5}$
$1≤ N≤ 10^{5}$
$1≤ A_{i}≤ N$
$A$ is a permutation
- The sum of $N$ over all test cases won't exceed $5\cdot 10^{5}$.

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

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

----- explanation 1 ------ 
Test case $1$: Blobo2 can perform $2$ swaps:
- For the first swap, Blobo2 can swap the first and second elements: $[\underline{5},\underline{2},1,4,3]$ $\rightarrow$ $[2,5,1,4,3]$
- For the second swap, he can swap the first and fourth elements: $[\underline{2},5,1,\underline{4},3]$ $\rightarrow$ $[4,5,1,2,3]$

Finally, Blobo2 will left shift the array two times which will lead to the lexicographically smallest permutation: $[1,2,3,4,5]$.

Test case $2$: Blobo2 can perform $2$ swaps:
- For the first swap, Blobo2 can swap the third and fifth elements: $[3,5,\underline{2},1,\underline{4}]$ $\rightarrow$ $[3,5,4,1,2]$
- For the second swap, he can swap the second and third elements: $[3,\underline{5},\underline{4},1,2]$ $\rightarrow$ $[3,4,5,1,2]$

Finally, Blobo2 will left shift the array two times which will lead to the lexicographically smallest permutation: $[1,2,3,4,5]$.

Difficulty: VERY_HARD
Tags: Greedy algorithms

Constructive algorithms

Ad-hoc
Skill types: Greedy algorithms

Starter code:

Solutions:
def solve(a, swaps):
	onepos = a.index(1)
	a = a[onepos:] + a[:onepos]
	for i in range(1, n):
		if swaps == 0 or a[i] == i + 1:
			continue
		pos = a.index(i + 1)
		swaps -= 1
		(a[i], a[pos]) = (a[pos], a[i])
	return a
for _ in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	ans = solve(a[:], 2)
	onepos = a.index(1)
	starts = []
	for i in range(n):
		if a[i] == 2:
			starts.append(i)
			starts.append((i - 1) % n)
			starts.append((i - 2) % n)
		if a[i] == 3:
			starts.append((i - 2) % n)
			starts.append(i)
	for x in starts:
		(a[onepos], a[x]) = (a[x], a[onepos])
		ans = min(ans, solve(a[:], 1))
		(a[onepos], a[x]) = (a[x], a[onepos])
	print(*ans)


def rotate(p):
	i = p.index(0)
	return p[i:] + p[:i]

def swap(p, i1, i2):
	i1 %= len(p)
	i2 %= len(p)
	rv = p.copy()
	(rv[i1], rv[i2]) = (rv[i2], rv[i1])
	return rv

def swaps(p):
	j = 1
	while j != len(p) and p[j] == j:
		j += 1
	if j == len(p):
		return [p]
	i1 = p.index(1)
	tries = [(j, p.index(j)), (0, i1 - 1), (0, i1), (0, -1), (0, -2), (i1, i1 - 1)]
	if len(p) > 2:
		tries.append((i1, p.index(2) - 1))
	return [rotate(swap(p, j, k)) for (j, k) in tries]
for _ in range(int(input())):
	input()
	v = [int(x) - 1 for x in input().split()]
	for x in min((r for q in swaps(rotate(v)) for r in swaps(q))):
		print(x + 1, end=' ')
	print()


def PRLI1(p, end='\n'):
	for x in p:
		print(x + 1, end=' ')
	print(end=end)

def LII1():
	return [int(x) - 1 for x in input().split()]

def rotate(p):
	i = p.index(0)
	return p[i:] + p[:i]

def swap(p, i1, i2):
	i1 %= len(p)
	i2 %= len(p)
	rv = p.copy()
	(rv[i1], rv[i2]) = (rv[i2], rv[i1])
	return rv

def swaps(p):
	j = 1
	while j != len(p) and p[j] == j:
		j += 1
	if j == len(p):
		return [p]
	try_1 = swap(p, j, p.index(j))
	try_2 = swap(p, 0, p.index(1) - 1)
	try_3 = swap(p, 0, p.index(1))
	try_4 = swap(p, 0, -1)
	try_5 = swap(p, 0, -2)
	try_6 = swap(p, p.index(1), p.index(1) - 1)
	rvs = [try_1, rotate(try_2), rotate(try_3), rotate(try_4), rotate(try_5), rotate(try_6)]
	if len(p) > 2:
		rvs.append(rotate(swap(p, p.index(1), p.index(2) - 1)))
	return rvs
for _ in range(int(input())):
	input()
	PRLI1(min([r for q in swaps(rotate(LII1())) for r in swaps(q)]))