Ensure that you are logged in and have the required permissions to access the test.

Word Rank

4.1

8 votes
Problem

You have been given a String S consisting of lowercase alphabets of the English Language. Now, we call a String W an anagram of another String S, if we can obtain String S be rearranging the characters of String W.

For example , the word "aba" has 3 anagrams i.e ["aba","aab","baa"] .

Now, you need to find all the disitnct anagrams of String S , sort them lexicographically and then print to output the anagram that occurs at the Nth position in this sorted order.

Can you do it ?

INPUT:

The first line of input contains the number of test cases denoted by T. The next T lines of input contain a string S and a number N.

OUTPUT:

You need to find and print the anagram of the given string which occurs at the $$N^{th} position when they are arranged in lexicographical order.

Constraints:

1T104

1|S|20

1N1018

Sample Input
4
bcda 20
cbad 5
ydvtrs 268
asfda 32
Sample Output
dacb
adbc
srdvyt
dfasa
Time Limit: 2
Memory Limit: 256
Source Limit:
Explanation

For case 2, one need to find the anagram of "cbad" which occurs at 5th place when arranged in lexicographical order.

The anagrams that occur at the first four positions are abcd, abdc, acbd and acdb respectively. The fifth anagram is adbc.

Contributers:
Editor Image

?