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 banned ingredients which cannot appear in food even in trace amounts. each ingredient has a serial number consisting of digits through . The declaration on each food package lists all the serial numbers of ingredients contained in the respective food item.
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 believing that it will make his job easier. He has borrowed a robot from his friend Slavko. The robot is programmed to check whether a serial number contains another serial number as a substring. Let us denote the length of by . The robot carries out search as follows:
- 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 length of the long serial number.
The second line of input contains digits from to , the long serial number.
The third line of input contains the positive integer , the number of banned ingredients.
Each of the following rows contains a single banned serial number,
A banned serial number will not exceed digits in length.
The total length of all banned serial numbers will not exceed digits.
Output
Output integers, one per line. Line must contain the dollar amount that Mirko needs to pay Slavko for the search for ingredient serial number .
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 comparisons.
Second serial number: tries first position, finding difference immediately ( comparison). Tries second position, finding difference on the fourth digit ( comparisons). Tries third position, finding difference immediately ( comparison). Tries fourth position, finding a match ( comparisons). Total: comparisons.
Third serial number: finds match immediately ( comparisons).
Fourth serial number: finds match at second position ( comparisons).
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
( comparison), 01
( comparison) and 1#
( comparisons). Total: comparisons.
Comments