Problem of the day
The first line contains a single integer ‘T’ representing the number of test cases.
The first line of each test case will contain an integer ‘N’ representing the length of the permutation.
The second line contains ‘N’ space-separated integers which are the elements of the permutation.
For each test case, print the elements of the lexicographically next greater permutation with a single space-separated. If lexicographically next greater permutation doesn’t exist, print the lexicographically smallest permutation.
Output for every test case will be printed in a separate line.
You do not need to print anything; It has already been taken care of.
1 <= T <= 50
1 <= N <= 10000
1 <= P[i] <= N
Time limit: 1 sec
2
3
1 2 3
5
2 3 1 4 5
1 3 2
2 3 1 5 4
In the first test case, the lexicographically next greater permutation is [1, 3, 2].
In the second test case, the lexicographically next greater permutation is [2, 3, 1, 4, 5].
2
2
1 2
3
3 1 2
2 1
3 2 1
In the first test case, the lexicographically next greater permutation is [2, 1].
In the second test case, the lexicographically next greater permutation is [3, 2, 1].