CF BUDDY
← Problems·

1187B · Letters Shop

1300 · binary search, implementation, strings

Problem: The letters shop showcase is a string ss, consisting of nn lowercase Latin letters. As the name tells, letters are sold in the shop.

Letters are sold one by one from the leftmost to the rightmost. Any customer can only buy some prefix of letters from the string ss.

There are mm friends, the ii-th of them is named tit_i. Each of them is planning to estimate the following value: how many letters (the length of the shortest prefix) would s/he need to buy if s/he wanted to construct her/his name of bought letters. The name can be constructed if each letter is presented in the equal or greater amount.

  • For example, for ss="arrayhead" and tit_i="arya" 55 letters have to be bought ("arrayhead").
  • For example, for ss="arrayhead" and tit_i="harry" 66 letters have to be bought ("arrayhead").
  • For example, for ss="arrayhead" and tit_i="ray" 55 letters have to be bought ("arrayhead").
  • For example, for ss="arrayhead" and tit_i="r" 22 letters have to be bought ("arrayhead").
  • For example, for ss="arrayhead" and tit_i="areahydra" all 99 letters have to be bought ("arrayhead").

It is guaranteed that every friend can construct her/his name using the letters from the string ss.

Note that the values for friends are independent, friends are only estimating them but not actually buying the letters.

Input Format: The first line contains one integer nn (1n21051 \le n \le 2 \cdot 10^5) — the length of showcase string ss.

The second line contains string ss, consisting of exactly nn lowercase Latin letters.

The third line contains one integer mm (1m51041 \le m \le 5 \cdot 10^4) — the number of friends.

The ii-th of the next mm lines contains tit_i (1ti21051 \le |t_i| \le 2 \cdot 10^5) — the name of the ii-th friend.

It is guaranteed that i=1mti2105\sum \limits_{i=1}^m |t_i| \le 2 \cdot 10^5.

Output Format: For each friend print the length of the shortest prefix of letters from ss s/he would need to buy to be able to construct her/his name of them. The name can be constructed if each letter is presented in the equal or greater amount.

It is guaranteed that every friend can construct her/his name using the letters from the string ss.

Sample Cases

Case 1

Input

9
arrayhead
5
arya
harry
ray
r
areahydra

Output

5
6
5
2
9

Similar problems

00:00:00
Loading editor…
Welcome! I'm your coding tutor for this problem. Use the chips below to reveal stored hints or get AI feedback on your code. I'll guide you step by step — never giving away the solution.

Sign in to unlock AI tutor feedback