JNEXT - Just Next !!!


DevG likes too much fun to do with numbers. Once his friend Arya came and gave him a challenge, he gave DevG an array of digits which is forming a number currently (will be called as given number). DevG was challanged to find the just next greater number which can be formed using digits of given number. Now DevG needs your help to find that just next greater number and win the challenge.

Input

The first line have t number of test cases (1 <= t <= 100). In next 2*t lines for each test case first there is number n (1 <= n <= 1000000) which denotes the number of digits in given number and next line contains n digits of given number separated by space.

Output

Print the just next greater number if possible else print -1 in one line for each test case.

Note : There will be no test case which contains zero in starting digits of any given number.

Example

Input:
2
5
1 5 4 8 3
10
1 4 7 4 5 8 4 1 2 6

Output:
15834
1474584162

hide comments
suraj_: 2016-08-13 15:17:39

did without stl

mkfeuhrer: 2016-06-17 00:00:35

too easy with stl:-)

satyamsingh: 2016-04-14 09:08:16

My 100th on spoj :)

giriprasad kemburu: 2016-02-23 03:12:46

Nice problem.solved without using stl

newbie: 2015-11-13 23:54:53

love u stl :) ac in one go

Deepak : 2015-11-02 09:01:12

Stl...:)

arpitkath: 2015-11-02 07:14:25

if u want to do without stl: http://www.nayuki.io/page/next-lexicographical-permutation-algorithm

MAYANK NARULA: 2015-10-31 15:05:26

Solving this problem w/o stl is pretty cool thing though..

sujit yadav: 2015-09-21 18:54:12

why this question has green rating ? extremely easy using c++ stl :O

loverboy: 2015-09-11 09:42:59

got A.C. in first attempt ,really easy use STL In c++


Added by:! include(L.ppt)
Date:2012-08-31
Time limit:1.297s
Source limit:50000B
Memory limit:1536MB
Cluster: Cube (Intel G860)
Languages:All except: ASM64
Resource:MNNIT OPC 31-08-2012