Problem hidden
This problem was hidden by Editorial Board member probably because it has incorrect language version or invalid test data, or description of the problem is not clear.

NEXTPERM - Next permutation

Given string n, your task is to print next permutation of string n.

Input

The first line of the standard input contains one integer t (t<101) which is the number of test cases.

In each of the next t lines there is one integer l (l<20) which is length of string n. Line below you will be given string n.

Output

For each string n, print the next permutation of string n or ERROR if there is no such a permutation.

Example

Input:
2
3
acb
3
cba
Output:
bac
ERROR


Added by:Piotr Kąkol
Date:2009-12-22
Time limit:4.900s
Source limit:50000B
Memory limit:1536MB
Cluster: Cube (Intel G860)
Languages:All except: SCM qobi
Resource:Permutation

hide comments
2013-11-03 15:18:29 Piotr KÄ…kol
Thanks for the info! I'll fix it along with LOOK_SAY.
Edit: Fixed and rejudged.

Last edit: 2013-12-07 00:20:24
2013-11-02 22:20:09 Mitch Schwartz
Test cases are weak; my submission 10406350 doesn't handle cases like cfedba properly but still passed.

Last edit: 2013-11-02 23:03:16
2012-09-01 19:12:04 Piotr KÄ…kol
@Avinash - Your answer for:
18
bdcofgpqarmhnelikj

is: bdcofgpqarmhneljki.
2012-09-01 13:11:07 AVINASH JAIN
Its giving wrong answer again and again. My program is running for the test cases given and for some random test cases. Please give some more test cases. Can n be zero(0).
2010-02-19 22:50:13 Piotr KÄ…kol
1) length < 20
2) Yes. For example succesive permutations of length 3 are:
abc
acb
bac
bca
cab
cba
Then You should print ERROR.
3) Yes, only lowercase (but ERROR is made of uppercase letters).
4) No, they can't.
5) Good luck. ;-)
2010-02-19 14:39:59 Alessandro Ferreira - UFMS
What's the limit for 'l'?
My task is to find the next permutation in lexicographic ordering ?
Does the string contain only lowercase letters? May appear repeated elements in string?

Last edit: 2010-02-19 18:33:11
© Spoj.com. All Rights Reserved. Spoj uses Sphere Engine™ © by Sphere Research Labs.