#codefestival2016qualBe. [codefestival_2016_qualB_e]Lexicographical disorder
[codefestival_2016_qualB_e]Lexicographical disorder
Problem Statement
There are strings of lowercase alphabet only. The -th string is . Every string is unique.
Provide answers for the queries below. The -th query has the following format:
Query: An integer and a string that results from permuting {a
b
z
} are given. Output the sequence of the string among the strings in lexicographical order when the literal sequence is .
Constraints
- is a string of lowercase alphabet.
- The sum of is no more than .
- Every is unique.
- For all , is a permutation of
abcd...z
.
Input
Inputs are provided from standard inputs in the following form.
: :
Output
Output lines.
On line , for the -th query, output an integer indicating the sequence of the string among the 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 , , and are thus , , and , 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 and are thus and , 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