Organizing CS contests didn't prove very lucrative for Mirko, so he has opened an ice cream and pastry shop. The business was flourishing until, one day, the European Union health inspection decided to pay him a visit.
A new directive specifies
Mirko must check whether any of his products has a banned ingredient serial number listed on its declaration. However, Mirko, being inept and reckless as always, decided to concatenate all the serial numbers into one long number with length
- First, it compares the segment of
from position to position , digit by digit, with the digits in . Comparison is stopped when the differing digit is found or when the segments are determined to be equal. If the segments are equal, the search is stopped and the match is reported. - If the segments are not equal, the procedure above is repeated with the segment from
to . If those segments aren't either, the search continues with the segments to , to etc. - If the robot doesn't have a sufficient number of digits to obtain a full segment of length
(for example, starting at character in a serial number with length , a segment with length is needed), it will pad the number with#
signs. For example, the segment of563232
from position to position is232####
. - If the robot reaches the end of the serial number (having tried out all
segments) without having found the absence of match is reported.
The robot takes one second for each comparison between two digits, and Slavko charges Mirko one dollar per second for using the robot.
Help Mirko determine how much money he'll have to pay Slavko for pattern matching!
Input
The first line of input contains the positive integer
The second line of input contains
The third line of input contains the positive integer
Each of the following
A banned serial number will not exceed
The total length of all banned serial numbers will not exceed
Output
Output
Scoring
In test data worth at least 20% of total points, the following constraints hold:
- length of a single banned ingredient serial number doesn't exceed
.
Sample Input 1
7
1090901
4
87650
0901
109
090
Sample Output 1
7
10
3
4
Explanation for Sample Output 1
First serial number: the robot finds differing first digits for every segment — a total of
Second serial number: tries first position, finding difference immediately (
Third serial number: finds match immediately (
Fourth serial number: finds match at second position (
Sample Input 2
10
5821052680
4
210526
2105
582
105268
Sample Output 2
8
6
3
9
Sample Input 3
3
001
1
11
Sample Output 3
4
Explanation for Sample Output 3
The robot compares the serial number 11
in order with segments 00
(01
(1#
(
Comments