JNEXT - Just Next !!!

题意翻译

给定 $T(T\leq 100)$ 组数据。 每组数据包含一个数 $N(N\leq 10^6)$ 以及 $N$ 个数 $a_1,a_2,...,a_n$ 现在要求你用这 $N$ 个数组成一个比 $\overline{a_1a_2...a_n}$ 大的第一个 $N$ 位数,若 $\overline{a_1a_2...a_n}$ 已经是最大的,输出 $-1$ 。 保证不会出现 $a_1=0$ 的情况。

题目描述

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.

输入输出格式

输入格式


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.

输出格式


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.

输入输出样例

输入样例 #1

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

输出样例 #1

15834
1474584162