BETA

Difficulty Level: Medium

Submissions: 5012 Accuracy:

34.14%

Next Permutation

Implement the next permutation, which rearranges numbers into the numerically next greater permutation of numbers.

If such arrangement is not possible, it must be rearranged as the lowest possible order ie, sorted in an ascending order.

For example:

1,2,3 → 1,3,2

3,2,1 → 1,2,3

 

Input:

The first line contains an integer T, depicting total number of test cases. 
Then following T lines contains an integer N depicting the size of array and next line followed by the value of array.


Output:

Print the array of next permutation in a separate line.


Constraints:

1 ≤ T ≤ 40
1 ≤ N ≤ 100
0 ≤ A[i] ≤ 100


Example:

Input
1
6
1 2 3 6 5 4
Output
1 2 4 3 5 6

** For More Input/Output Examples Use 'Expected Output' option **

Set Default Code

					   
It is recommended to 'Compile & Test' your code before clicking 'Submit'!

Compilation/Execution Result:

Need help with your code? Please use ide.geeksforgeeks.org, generate link and share the link here.