#P1098F. Ж-function
Ж-function
Description
The length of the longest common prefix of two strings and is defined as the maximum such that equals . Let's denote the longest common prefix of two strings and as .
Z-function of a string is a sequence of integers , where . Ж-function of a string is defined as .
You're given a string and queries. Each query is described by two integers and , where . The answer for the query is defined as Ж-function of the string .
The first line contains the string , consisting of lowercase English letters (). The second line contains one integer — the number of queries (). Each of the following lines contains two integers and , describing the query ().
For every query output one integer: the value of Ж-function of the corresponding substring.
Input
The first line contains the string , consisting of lowercase English letters (). The second line contains one integer — the number of queries (). Each of the following lines contains two integers and , describing the query ().
Output
For every query output one integer: the value of Ж-function of the corresponding substring.
Note
In the first sample case there are four queries:
- the first query corresponds to the substring bb, and its Ж-function equals ;
- the second query corresponds to the substring abb, and its Ж-function equals ;
- the third query corresponds to the substring b, and its Ж-function equals .
- the fourth query corresponds to the substring abdd, and its Ж-function equals .