7 Maratona NexTI - 2018

Start

2018-05-25 17:00 UTC

7 Maratona NexTI - 2018

End

2018-05-28 17:00 UTC
The end is near!
Contest is over.
Not yet started.
Contest is starting in -360 days 9:22:56

72:00:00

0:00:00

Problem ELexicography

An anagram of a string is any string that can be formed using the same letters as the original. (We consider the original string an anagram of itself as well.) For example, the string ACM has the following 6 anagrams, as given in alphabetical order:

ACM
AMC
CAM
CMA
MAC
MCA


As another example, the string ICPC has the following 12 anagrams (in alphabetical order):

CCIP
CCPI
CICP
CIPC
CPCI
CPIC
ICCP
ICPC
IPCC
PCCI
PCIC
PICC


Given a string and a rank $K$, you are to determine the $K^{\text {th}}$ such anagram according to alphabetical order.

Input

The input is composed of $T$ test cases ($1 \leqslant T \leqslant 50$).

Each test case will be designated on a single line containing the original word followed by the desired rank $K$. Words will use uppercase letters (i.e., A through Z) and will have length at most 16. The value of $K$ will be in the range from 1 to the number of distinct anagrams of the given word.

A line of the form # 0 designates the end of the input.

Warning: The value of $K$ could be almost $2^{45}$ in the largest tests, so you should use type long in Java, or type long long in C++ to store $K$.

Output

For each test, display the $K^{\text {th}}$ anagram of the original string.

Sample Input 1 Sample Output 1
ACM 5
ICPC 12
REGION 274
# 0

MAC
PICC
IGNORE