graphlogo

Wydział Matematyki i Informatyki

Uniwersytetu Mikołaja Kopernika w Toruniu

1inf 2024/2025 Programowanie I - LA*

[F2] Lexicographic Order
Języki: c
Limit czasu: 3.0 s
Limit pamięci: 0 MB

The alphabet of a certain alien language consists of n distinct symbols. The symbols are like the letters of English alphabet but their ordering is different. You want to know the original order of the symbols in that particular alphabet. You have a string consists of all the letters of that alphabet and you know that this is the k-th (1 based) lexicographic permutation of these symbols. You have to arrange these symbols in lexicographic order of that language.


Input

The first line of input will contain an integer T (T ≤ 5000) which denotes the number of test cases. Each of the following T lines contains a string s and an integer k. The string will be of length n (1 ≤ n ≤ 20) and will consist of lowercase letters only. All the letters in the string will be distinct. The value of k will be in the range (1 ≤ k ≤ n!).


Output

For each line of input output the case number and a string which contains the letters in lexicographic order in that language.


Sample Input
3
bdac 11
abcd 5
hjbrl 120

Output for Sample Input
Case 1: abcd
Case 2: acdb
Case 3: lrbjh

Note

The first input resembles the original order of English alphabet. Here are the lexicographic permutations:

abcd1 cabd13
abdc2 cadb14
acbd3 cbad15
acdb4 cbda16
adbc5 cdab17
adcb6 cdba18
bacd7 dabc19
badc8 dacb20
bcad9 dbac21
bcda10 dbca22
bdac11 dcab23
bdca12 dcba24
Powrót
© 2009-2020 • ZawodyWeb Team
IKS - Inwestycja w Kierunki Strategiczne na Wydziale Matematyki i Informatyki UMK

Projekt współfinansowany ze środków Unii Europejskiej w ramach Europejskiego Funduszu Społecznego