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
varun1997:
20201006 12:06:46
Stack is helpful in finding just next smaller element then what is already in tos! May this hint helps!


gauris_17:
20200921 10:53:33
How to solve it using stack?


scrapperdb:
20200903 15:19:49
@zerothking @mr_code_art this can be solved using queue. 

scrapperdb:
20200903 15:05:52
@nayan_awati01 Output for your test case must be 768 

nayan_awati01:
20200902 18:02:14
i think you must try 687


uptanuj:
20200828 17:53:39
run for


tanjilahmed:
20200817 16:00:03
ck_17 for your TC my ans 2837799... 

nikhil0010:
20200806 14:45:18
I'm a homo Last edit: 20200806 22:48:07 

rehank478:
20200806 07:24:22
Try to learn how next_permutation works then implement it by your own.


surly:
20200729 23:16:23
You can try these

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