시간 제한 메모리 제한 제출 정답 맞은 사람 정답 비율
1 초 256 MB 8 5 5 83.333%

문제

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 Kth such anagram according to alphabetical order.

입력

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 245 in the largest tests, so you should use type long in Java, or type long long in C++ to store K.

출력

For each test, display the Kth anagram of the original string.

예제 입력

ACM 5
ICPC 12
REGION 274
# 0

예제 출력

MAC
PICC
IGNORE

힌트