Task
You are given a string of distinct letters, consisting of the first letters of the alphabet, and a string of letters, which contains only letters from the first letters of the alphabet.
There are queries of the form , such that , to which you must respond with the number of sequences that exist such that and for any . Since this number can be very large, only the remainder of this number divided by is required.
Input data
The first line contains two integers, and . The next line contains a string of distinct letters. The third line contains a string of letters. The fourth line contains a single integer, . The following lines contain two integers and , representing the data for the -th query.
Output data
On the -th of the lines, there will be a single integer, the answer to the -th query.
Constraints and clarifications
# | Points | Constraints |
---|---|---|
0 | 0 | Example |
1 | 10 | |
2 | 12 | |
3 | 5 | |
4 | 23 | |
5 | 50 | No additional constraints |
Example
stdin
2 12
ab
abababababab
3
3 8
2 5
6 11
stdout
6
1
3
Explanation
For the first query, the subsequence is ababab
, and ab
appears times: ABabab
, AbaBab
, AbabaB
, abABab
, abAbaB
, ababAB
.
For the second query, the subsequence is baba
, and ab
appears only once: bABa
.
For the third query, the subsequence is bababa
, and ab
appears times: bABaba
, bAbaBa
, babABa
.