You are given a deterministic finite automaton that accepts
the language . You will then be given
queries asking for the number of words in with a specified length,
which you should answer in the order they are given.
Input
The input contains the description of a deterministic finite
automaton.
The first line contains four positive integers , , , and , where is the number of states,
is the size of the
alphabet, is the
initial state, and is
the number of final states. The second line consists of a
string of distinct symbols, each of which is
a lowercase english character. The third line consists of
distinct positive
integers, the set of final states. Then lines follow, each with
positive integers,
describing the symbol table. The -th integer on the -th of those lines represents the
state transitioned to from state upon reading .
Each state is an integer between and . It is guaranteed that
,
, and
.
Finally, the input will contain an integer and then lines. The -th line will contain , for , querying for the
number of words with length in . You may assume that
for each .
Output
For each of the
queries, output the number of words with the specified length
in . As these
numbers can be quite large, output them modulo .
Sample Input 1 |
Sample Output 1 |
5 2 1 2
ab
4 5
2 3
3 4
4 5
5 4
4 4
5
0
1
2
3
4
|
0
0
3
8
16
|
Sample Input 2 |
Sample Output 2 |
2 4 1 1
acgt
1
2 2 2 2
2 2 2 2
3
0
1
2
|
1
0
0
|