#include <iostream>
#include <fstream>
#include <map>
using namespace std;
const int MAX_LEN = 16;
ifstream fin("lex.in");
long long factorial(int k) {
long long ans = 1;
for (int j=2; j <= k; j++)
ans *= j;
return ans;
}
long long count(int length, const map<char,int>& freq) {
long long answer = factorial(length);
for (map<char,int>::const_iterator it = freq.begin(); it != freq.end(); ++it)
if (it->second > 1)
answer /= factorial(it->second);
return answer;
}
map<char,int> profile(const string& s) {
map<char,int> counts;
for (int j=0; j < s.size(); j++)
counts[s[j]]++;
return counts;
}
string solve(const string& original, long long rank) {
string answer;
map <char,int> freq = profile(original);
for (int length = original.size(); length > 0; --length) {
for (map<char,int>::iterator it = freq.begin(); it != freq.end(); ++it) {
if (it->second > 0) {
freq[it->first]--;
long long sub = count(length-1, freq);
if (sub >= rank) {
answer += it->first;
break; } else {
freq[it->first]++;
rank -= sub;
}
}
}
}
return answer;
}
void validateInput(const string& original, long long K) {
if (original.size() > MAX_LEN)
cerr << "ERROR: string is too long" << endl;
for (int j=0; j<original.size(); j++) {
if (original[j] < 'A' || original[j] > 'Z')
cerr << "ERROR: illegal character in string";
}
if (K < 1 || K > count(original.size(), profile(original)))
cerr << "ERROR: illegal rank " << K << endl;
}
int main() {
while (true) {
string orig;
long long K;
fin >> orig >> K;
if (K == 0) break;
cout << solve(orig, K) << endl;
validateInput(orig,K); }
}