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 K-th 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 distinct 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 < 231).

Output

Output consists of Q lines, the i-th contains a string which is the answer to the i-th asked question.

Example

Input:
aaa
2
2
3

Output: aa
aaa

Edited: Some input file contains garbage at the end. Do not process them.


Added by:Damir Ferizovic
Date:2010-09-02
Time limit:1s
Source limit:50000B
Memory limit:1536MB
Cluster: Cube (Intel G860)
Languages:All except: NODEJS OBJC VB.NET
Resource:Own Problem

hide comments
2021-07-19 19:00:10 Shi Chen
Why not let K be a 64-bit integer?

Last edit: 2021-07-19 19:03:43
2020-10-12 20:21:25
basic problem... try using suffix array or suffix automaton
2020-04-14 09:30:59
Ac Finally :)
2020-03-23 05:30:37
I should not put function strlen() in the for loop.
2020-03-22 09:06:04
Why I got a TLE with SAM ?
2019-06-05 10:11:38
To print the output string for a certain query, use substr function of string class,
Do not try to print using a for loop. It cost me a TLE.
2017-09-25 16:40:32
I got AC if I ignore the case that k >= (distinct substrings)...
2017-09-07 14:05:16
My 150th :-)
AC in one go!!
2017-06-04 10:50:13 Thotsaphon Thanatipanonda
output should be starting position and length of result string, not the whole result string.
2017-04-02 05:15:26
Finally get AC with SAM!!!
© Spoj.com. All Rights Reserved. Spoj uses Sphere Engine™ © by Sphere Research Labs.