H - Percepts of Atcoder Editorial /

Time Limit: 3 sec / Memory Limit: 1024 MB

配点:14001400

問題文

E869120 は, 文字列を square1001 に今年から QQ 年間, 毎年プレゼントすることにした.
彼は, 文字列 SS が大好きであった. そのため, プレゼントする文字列は, 文字列 SS から取ることに決めた. また, 古から伝わる, AtCoder 社の教えに基づいて, プレゼントする文字列を決めることにした.
AtCoder 社の教えは, 以下のようなものである.

  • 友達に文字列をプレゼントするときは, 以下の方法で文字列を決めなさい. ここでは, あなたの大好きな文字列を SS とおく.
  • 文字列 SS の部分列の集合を TT とおく. ただし, 文字列 SS の部分列とは, SS から 00 個以上の文字を取り去ってできた 11 文字以上の長さの文字列のことを指す.
  • 部分列の集合 TT を辞書順でソートしたときに, KK 番目となる文字列をプレゼントするべきである.

例えば, 大好きな文字列が aqua である場合, TT は [a,aa,aq,aqa,aqu,aqua,au,aua,q,qa,qu,qua,u,ua] となる. K=10K=10 のとき, 辞書順で 1010 番目となる qa をプレゼントするべきである.

しかし, KK の値は年ごとに変わってしまう.
そこで, ii 年目の KK の値 KiK_i が与えられるので, 各年ごとのプレゼントすべき文字列の最後の pip_i 文字を求めてほしい.

入力

入力は以下の形式で標準入力から与えられる.

SS
QQ
K1K_1 p1p_1
K2K_2 p2p_2
K3K_3 p3p_3
...
KQK_Q pQp_Q

出力

QQ 行出力すること.
ii 行目には, ii 年目にプレゼントすべき文字列の最後の pip_i 文字を出力すること. ただし, 該当する文字列が存在しない場合 "-1" と出力せよ.
また, プレゼントすべき文字列が pip_i 文字未満の場合, プレゼントすべき文字列をそのまま出力せよ. 例えば, プレゼントすべき文字列が "abcde" であり, pi=7p_i=7 のとき, この行には "abcde" と出力すること.


制約

  • QQ11 以上 100 000100 \ 000 以下の整数.
  • SS は英小文字から成る, 11 文字以上 300 000300 \ 000 文字以下の文字列.
  • pip_i11 以上 S|S| 以下の整数.
  • p1+p2+...+pQ1 000 000p_1+p_2+...+p_Q \leq 1 \ 000 \ 000
  • KiK_i11 以上 101810^{18} 以下の整数.

小課題

小課題 11 [210210 点]

  • Q=1Q=1 を満たす.

小課題 22 [320320 点]

  • Ki1 000 000K_i \leq 1 \ 000 \ 000 を満たす.

小課題 33 [870870 点]

  • 追加の制約はない.

入力例 1Copy

Copy
aqua
16
1 10
2 10
3 10
4 10
5 10
6 10
7 10
8 10
9 10
10 10
11 10
12 10
13 10
14 10
15 10
16 10

出力例 1Copy

Copy
a
aa
aq
aqa
aqu
aqua
au
aua
q
qa
qu
qua
u
ua
-1
-1

問題文の例の通りです.


入力例 2Copy

Copy
tourist
2
76 10
76 4

出力例 2Copy

Copy
tourist
rist

22 個目のクエリにおいて, "tourist" の後ろ 44 文字は "rist" なので, これを出力すること.


入力例 3Copy

Copy
eightsixnineonetwozero
6
869120 100
869120 10
869120 8
869120 6
869120 4
869120 2

出力例 3Copy

Copy
eihinieonetwoze
ieonetwoze
onetwoze
etwoze
woze
ze

score: 14001400 points

Problem Statement

E869120 the Coder decided to give a string to square1001 as a present each year for the next QQ years.
Since E869120's favorite string is SS, he also decided to "pick" the string, which he'll give as a present, from SS.
Moreover, there's "AtCoder percepts" which has been told from ancient times, so he also decided to determine the string based on this percepts.

The contents of "AtCoder percepts" is as follows:

  • When you will give a string as a present, you should decide the string as follows.
  • Let TT be the set of substring (not necessarily contiguous) of SS.
  • You should give KK-th string of TT in lexicographical order.

For example, if his favorite string is aqua, TT will be [a,aa,aq,aqa,aqu,aqua,au,aua,q,qa,qu,qua,u,ua]. If K=10K=10, you should give qa as a present, because qa is the 1010-th string in TT.

However, the value of KK will change every year. The ii-th year of KK will be KiK_i.
Please answer the last pip_i characters of string which you should give to square1001 in ii-th year.

Input

Input is given from Standard Input in the following format:

SS
QQ
K1K_1 p1p_1
K2K_2 p2p_2
K3K_3 p3p_3
...
KQK_Q pQp_Q

Output

  • Print QQ lines.
  • In ii-th line, print the last pip_i characters of substring of the string which he should give in ii-th year.
  • If there is no string which is applicable, print "-1".
  • If the string which he should give is less than pip_i characters, print the string. For example, if pi=7p_i=7 and string = "abcde", print "abcde".

Constraints

  • 1Q100 0001 \leq Q \leq 100 \ 000.
  • 1S300 0001 \leq |S| \leq 300 \ 000.
  • SS consists of lowercase alphabets: from a to z.
  • 1piS1 \leq p_i \leq |S|.
  • p1+p2+...+pN1 000 000p_1+p_2+...+p_N \leq 1 \ 000 \ 000.
  • 1Ki10181 \leq K_i \leq 10^{18}.
  • All input values are integers.

Subtasks

Subtask 11 [210210 points]

  • Q=1Q=1.

Subtask 22 [320320 points]

  • Ki1 000 000K_i \leq 1 \ 000 \ 000.

Subtask 33 [870870 points]

  • There is no additional constraints.

Sample Input 1Copy

Copy
aqua
16
1 10
2 10
3 10
4 10
5 10
6 10
7 10
8 10
9 10
10 10
11 10
12 10
13 10
14 10
15 10
16 10

Sample Output 1Copy

Copy
a
aa
aq
aqa
aqu
aqua
au
aua
q
qa
qu
qua
u
ua
-1
-1

Please look the example in the problem statement.


Sample Input 2Copy

Copy
tourist
2
76 10
76 4

Sample Output 2Copy

Copy
tourist
rist

In 22-nd query, the answer is "tourist". But you should output the last 44 characters: "rist", because p2=4p_2 = 4.


Sample Input 3Copy

Copy
eightsixnineonetwozero
6
869120 100
869120 10
869120 8
869120 6
869120 4
869120 2

Sample Output 3Copy

Copy
eihinieonetwoze
ieonetwoze
onetwoze
etwoze
woze
ze


2025-04-01 (Tue)
01:03:08 +00:00