SUBLEX  Lexicographical Substring Search
Little Daniel loves to play with strings! He always finds different ways to have fun with strings! Knowing that, his friend Kinan decided to test his skills so he gave him a string S and asked him Q questions of the form:
If all distinct substrings of string S were sorted lexicographically, which one will be the Kth smallest?
After knowing the huge number of questions Kinan will ask, Daniel figured out that he can't do this alone. Daniel, of course, knows your exceptional programming skills, so he asked you to write him a program which given S will answer Kinan's questions.
Example:
S = "aaa" (without quotes)
substrings of S are "a" , "a" , "a" , "aa" , "aa" , "aaa". The sorted list of substrings will be:
"a", "aa", "aaa".
Input
In the first line there is Kinan's string S (with length no more than 90000 characters). It contains only small letters of English alphabet. The second line contains a single integer Q (Q <= 500) , the number of questions Daniel will be asked. In the next Q lines a single integer K is given (0 < K < 2^31).
Output
Output consists of Q lines, the ith contains a string which is the answer to the ith asked question.
Example
Input:
aaa
2
2
3
Output: aa
aaa
Edited: Some input file contains garbage at the end. Do not process them.
hide comments
Muhammad Rifayat Samee (Sanzee):
20150427 12:05:04
getting TLE using suffix array O(n * (lgn)^2) .....


hzf:
20150125 02:54:52
SAM is expected! 

AAKASH SHINGHAL:
20150123 19:25:13
What's the possible reason of NZEC error in JAVA? Last edit: 20150123 19:30:24 

Buda IM (retired):
20111108 16:54:11
I got AC without handling that kind of cases 

Axel Brzostowski:
20111104 08:30:01
What do we suppose to answer if K > #(distinct substrings)? It seems like it could happen... Last edit: 20111104 19:30:54 

Seshadri R:
20100918 16:20:17
@Damir: Thanks for providing the clarity. It was not obvious from the example given. 

Damir Ferizovic:
20100916 18:35:46
@Seshadri: what you wrote are permutations, not substring. For "abc" the substrings would be:


Seshadri R:
20100916 04:30:24
If "abc" is the main string, would "abc", "acb", "bac", "bca", "cba" and "cab" be all considered as substrings for the main string? Last edit: 20100916 04:31:37 
Added by:  Damir Ferizovic 
Date:  20100902 
Time limit:  0.149s0.299s 
Source limit:  50000B 
Memory limit:  1536MB 
Cluster:  Cube (Intel G860) 
Languages:  All except: NODEJS objc VB.net 
Resource:  Own Problem 