Module: Permutations


Problem

1 /5


Construct the next anagram from a string

Problem

For a given word (sequence of lowercase Latin letters), print the next word (in lexicographical order) that can be obtained from the given one by permuting letters (an anagram). If the given word is already the last among all its anagrams, then print the first possible (in lexicographical order) anagram.

Input
The first line contains the number N - the number of words. This is followed by a sequence of N words, one word per line. The length of one word does not exceed 50 characters.

Imprint
Need to output  result for each input word.
 

 

Examples
# Input Output
1 4
aab
aba
baa
aaa
aba
baa
aab
aaa