Next Permutation

No attempts yetTime limit1sMemory limit128 MB

Problem

You are given a positive integer $A$. Among all integers whose digits are a rearrangement (a permutation) of the digits of $A$, find the smallest one that is strictly greater than $A$ — in other words, the next larger permutation of $A$; call it $B$.

For example, if $A = 2413$, the next larger permutation is $2431$.

If $A$ is already the largest number that can be formed from its digits (no larger permutation exists), print USELESS instead.

Input

The first line contains an integer $T$, the number of test cases. Each of the following $T$ lines contains a single integer $A$ ($A \le 2{,}000{,}000$).

Output

For each test case, print the next larger permutation $B$ of $A$ on its own line. If it does not exist, print USELESS.