Given a string of characters, we can permute the individual characters to make new strings. At first we order the string into alphabetical order. Then we start permuting it.
For example the string 'abba' gives rise to the following 6 distinct permutations in alphabetical order:
- aabb
- abab
- abba
- baab
- baba
- bbaa
Given a string, you have to find the nth permutation for that string. For the above case 'aabb' is the 1st and 'baab' is the 4th permutation.
Input
Input starts with an integer T (≤ 200), denoting the number of test cases.
Each case contains a non empty string of lowercase letters with length no more than 20 and an integer n (0 < n < 231).
Output
For each case, output the case number and the nth permutation. If the nth permutation doesn't exist print Impossible
.
Sample
Sample Input | Sample Output |
---|---|
3 aabb 1 aabb 6 aabb 7 | Case 1: aabb Case 2: bbaa Case 3: Impossible |