Frankenstein's Banner
View as PDFThe MAPS Academy team is preparing the captions for an Olympic archery stream. The full chant for the day is one long lowercase string , but the director keeps asking for short slices of it between ends, so the team has to assemble them from prepared banner strips.
The team has reusable banner templates, written as strings
. There are infinitely many copies of every template, and using one copy has cost
.
Before using a copy, the team may cut off any suffix, possibly the empty suffix. Equivalently, one copy may contribute any prefix of its template. The chosen prefixes may then be concatenated in any order.
For each request , determine the minimum number of template copies needed to form exactly the substring
. The indices are zero-based, and the right endpoint is not included. If the substring cannot be formed, output
-1.
Input
The first line contains two integers (
) and
(
): the number of banner templates and the number of requests.
The next lines contain the nonempty strings
. All strings consist only of lowercase English letters. The total length of all strings
is at most
.
The next line contains the string , consisting only of lowercase English letters (
).
Each of the next lines contains two integers
and
(
), describing one requested substring.
Output
Print lines. The
-th line must contain the answer to query
, or
-1 if it is impossible.
Example 1
Input
3 8
aim
arrow
gold
aimarrowgoldaimx
0 3
3 6
0 8
0 15
8 15
1 3
15 16
0 16
Output
1
1
2
4
2
-1
-1
-1
Comments