// Arup Guha
// 1/31/2015
// Solution to 2014 MCPC Problem C: Lexicography

import java.util.*;

public class c {

	// Easier since we want different functions to change this..
	public static long rank;

	public static void main(String[] args) {

		Scanner stdin = new Scanner(System.in);
		String str = stdin.next();
		rank = stdin.nextLong() - 1;

		while (rank != -1) {

			// Calculate frequency array.
			int[] freq = new int[26];
			for (int i=0; i<str.length(); i++)
				freq[str.charAt(i)-'A']++;
			int total = str.length();

			String ans = "";

			// Peel off each letter, one by one from front.
			while (total > 0) {
				ans = ans + getFirstLetter(freq, total);
				total--;
			}

			// Print the answer.
			System.out.println(ans);

			// Get next case.
			str = stdin.next();
			rank = stdin.nextLong() - 1;
		}
	}

	// Returns n factorial.
	public static long fact(int n) {
		long ans = 1;
		for (int i=1; i<=n; i++)
			ans *= i;
		return ans;
	}

	// Returns the multinomial coefficient dictated by freq. total is the sum of items in freq.
	public static long combos(int[] freq, int total) {
		long ans = fact(total);
		for (int i=0; i<26; i++)
			ans /= fact(freq[i]);
		return ans;
	}

	public static String getFirstLetter(int[] freq, int total) {

		// Go through each letter.
		for (int i=0; i<26; i++) {
			if (freq[i] > 0) {

				// Take this letter out of the count and count perms left.
				freq[i]--;
				long tmp = combos(freq, total-1);

				// This is our letter.
				if (tmp > rank) return ""+ ((char)('A'+i));

				// It's not, so put it back in and sub out of rank.
				freq[i]++;
				rank -= tmp;
			}
		}

		// Should never get here.
		return "@";
	}
}