#codefestival2016qualBe. [codefestival_2016_qualB_e]Lexicographical disorder

[codefestival_2016_qualB_e]Lexicographical disorder

Problem Statement

There are NN strings of lowercase alphabet only. The ii-th string is SiS_i. Every string is unique.

Provide answers for the QQ queries below. The ii-th query has the following format:

Query: An integer kik_i and a string pi,1pi,2...pi,26p_{i,1}p_{i,2}...p_{i,26} that results from permuting {a,,b,...,,...,z} are given. Output the sequence of the string SkiS_{k_i} among the NN strings in lexicographical order when the literal sequence is pi,1<pi,2<...<pi,26p_{i,1}<p_{i,2}<...<p_{i,26}.

Constraints

  • 1N,Q1000001 ≦ N,Q ≦ 100000
  • 1Si(1iN)1 ≦ |S_i| (1 ≦ i ≦ N)
  • Si(1iN)S_i (1 ≦ i ≦ N) is a string of lowercase alphabet.
  • The sum of Si|S_i| is no more than 400000400000.
  • Every SiS_i is unique.
  • 1kiN(1iQ)1 ≦ k_i ≦ N (1 ≦ i ≦ Q)
  • For all 1iQ1 ≦ i ≦ Q, pi,1pi,2...pi,26p_{i,1}p_{i,2}...p_{i,26} is a permutation of abcd...z.

Input

Inputs are provided from standard inputs in the following form.

NN S1S_1 : SNS_N QQ k1k_1 p1,1p1,2...p1,26p_{1,1}p_{1,2}...p_{1,26} : kQk_Q pQ,1pQ,2...pQ,26p_{Q,1}p_{Q,2}...p_{Q,26}

Output

Output QQ lines.

On line ii, for the ii-th query, output an integer indicating the sequence of the string SkiS_{k_i} among the NN strings in lexicographical order.


Sample Input 1

5
aa
abbaa
abbba
aaab
aaaaaba
5
1 abcdefghijklmnopqrstuvwxyz
2 bacdefghijklmnopqrstuvwxyz
3 abcdefghijklmnopqrstuvwxyz
4 bacdefghijklmnopqrstuvwxyz
5 abcdefghijklmnopqrstuvwxyz

Sample Output 1

1
2
5
4
2

When the literal sequence is a << b, sorting the input strings in lexicographical order yields aa, aaaaaba, aaab, abbaa, abbba. The answers to queries 11, 33, and 55 are thus 11, 55, and 22, respectively.

When the literal sequence is b << a, sorting the input strings in lexicographical order yields abbba, abbaa, aa, aaab, aaaaaba. The answers to queries 22 and 44 are thus 22 and 44, respectively.


Sample Input 2

8
abrakatabra
abadaba
abracadabra
atcoder
grand
contest
ababa
a
6
3 abcdefghijklmnopqrstuvwxyz
6 qwertyuiopasdfghjklzxcvbnm
8 poiuytrewqlkjhgfdsamnbvcxz
2 qazwsxedcrfvtgbyhnujmikolp
1 plokmijnuhbygvtfcrdxeszwaq
4 mnbvcxzasdfghjklpoiuytrewq

Sample Output 2

4
8
2
3
4
7