Geek Sundaram and his Wheel of Fortune

All submissions for this problem are available.
Problem description
Anyone who knows Geek Sundaram knows that he has a penchant for games of chance. So, it should come as no surprise that he designed one of his own called Wheel of Fortune (not the popular game show, though it could have inspired the name, no one really understands the workings of Geek’s mind), which as its name suggests, is a game of chance.
Although Wheel of Fortune is worth explaining, we are not going to as this problem concerns the physical wheel he uses for playing the Wheel of Fortune. We shall call it simply the ‘Wheel’ from now on. The Wheel is roulettestyled and contains N equal slices (having the same arc length, central angle and area). Each slice is marked with a character from an alphabet S.
In the initial configuration of the Wheel, S is equal to N and each slice is marked with a unique character from S. Apart from the characters marked on them, the slices are indistinguishable from each other. So, one has to look at the character in the selected slice to infer the outcome of a spin. In this configuration, the number of equally likely outcomes that could be represented in the Wheel is S.
In the above simple depiction of a Wheel, N = S = 8 and S = {‘a’, ‘f’, ‘t’, ‘q’, ‘p’, ‘r’, ‘e’, ‘j’} in the initial configuration. Each character represents one of the eight outcomes.
Geek realises in order to accommodate more outcomes in the Wheel, the size of the alphabet S has to be increased. Geek being a geek, came up with a way to represent more outcomes in the Wheel without increasing the alphabet size. His solution is to club L contiguous slices together and read the characters clockwise to form a string and that string could represent an outcome. For example, in the given figure, for L = 3, strings that could be formed are “aft”, “ftq”, “tqp”, “qpr”, “pre”, “rej”, “eja” and “jaf”.
For a wheel with a given value of L, if two strings (of length L) read in such a way from different positions in the Wheel are the same, it is said to be invalid. Note that for any L, the number of resultant strings (outcomes) possible is N, the number of slices. Also, in the initial configuration L = 1.
You are given S and L. Your task is to help Geek design a valid Wheel so that as much number of outcomes as possible can be represented by that Wheel.
Input
Each test case consists of two lines.
The first line contains a string of lowercase English letters without repetition. The letters in the string form the alphabet S. If a letter appears before another in the string, then the former has alphabetical precedence over the latter.
The second line contains an integer denoting L.
Output
The string representation of a Wheel is obtained by writing the characters in all its slices once, starting from the slice which will result in the lexicographically smallest string. For a given S and L, there may be multiple Wheels which can represent the maximum possible number of outcomes. For each test case, among those multiple Wheels, print the string representation of the Wheel for which it is the lexicographically smallest.
Constraints
 1 < S < 10
 1 <= L <= 10
 The output string will not be longer than 10^6 characters.
Examples
Input 1: aftqprej 1 Output 1: aftqprej
Input 2: ab 3 Output 2: aaababbb
Input 3: aftqprej 2 Output 3: aafataqaparaeajfftfqfpfrfefjttqtptrtetjqqpqrqeqjpprpepjrrerjeejj
Explanation
Example case 1. This is the initial configuration discussed in the problem.
Example case 2. Strings of length 3 which can be formed from the Wheel "aaababbb" are "aaa", "aab", "aba", "bab", "abb", "bbb", "bba" and "baa", each one distinct. In fact, all strings of length 3 on alphabet {'a', 'b'} are listed. Another wheel possible is "aaabbbab" but it is not the lexicographically smallest.
Author:  bharath96 
Tags  bharath96 
Date Added:  15032017 
Time Limit:  1 sec 
Source Limit:  50000 Bytes 
Languages:  C, CPP14, JAVA, PYTH, PYTH 3.6, PYPY, CS2, PAS fpc, PAS gpc, RUBY, PHP, GO, NODEJS, HASK, SCALA, D, PERL, FORT, WSPC, ADA, CAML, ICK, BF, ASM, CLPS, PRLG, ICON, SCM qobi, PIKE, ST, NICE, LUA, BASH, NEM, LISP sbcl, LISP clisp, SCM guile, JS, ERL, TCL, PERL6, TEXT, SCM chicken, CLOJ, FS 
Comments
 Please login at the top to post a comment.
SUCCESSFUL SUBMISSIONS
Fetching successful submissions