#P1098F. Ж-function

    ID: 4825 Type: RemoteJudge 6000ms 512MiB Tried: 0 Accepted: 0 Difficulty: 10 Uploaded By: Tags>string suffix structuresstrings*3500

Ж-function

Description

The length of the longest common prefix of two strings s=s1s2sns=s_1 s_2 \ldots s_n and t=t1t2tmt = t_1 t_2 \ldots t_m is defined as the maximum kmin(n,m)k \le \min(n, m) such that s1s2sks_1 s_2 \ldots s_k equals t1t2tkt_1 t_2 \ldots t_k. Let's denote the longest common prefix of two strings ss and tt as lcp(s,t)lcp(s,t).

Z-function of a string s1s2sns_1 s_2 \dots s_n is a sequence of integers z1,z2,,znz_1, z_2, \ldots, z_n, where zi=lcp(s1s2sn,  sisi+1sn)z_i = lcp(s_1 s_2 \ldots s_n,\ \ s_i s_{i+1} \dots s_n). Ж-function of a string ss is defined as z1+z2++znz_1 + z_2 + \ldots + z_n.

You're given a string s=s1s2sns=s_1 s_2 \ldots s_n and qq queries. Each query is described by two integers lil_i and rir_i, where 1lirin1 \le l_i \le r_i \le n. The answer for the query is defined as Ж-function of the string slisli+1sris_{l_i} s_{l_i +1} \ldots s_{r_i}.

The first line contains the string ss, consisting of lowercase English letters (1s2000001 \leq |s| \leq 200\,000). The second line contains one integer qq — the number of queries (1q2000001 \leq q \leq 200\,000). Each of the following qq lines contains two integers lil_i and rir_i, describing the query (1liris1 \leq l_i \leq r_i \leq |s|).

For every query output one integer: the value of Ж-function of the corresponding substring.

Input

The first line contains the string ss, consisting of lowercase English letters (1s2000001 \leq |s| \leq 200\,000). The second line contains one integer qq — the number of queries (1q2000001 \leq q \leq 200\,000). Each of the following qq lines contains two integers lil_i and rir_i, describing the query (1liris1 \leq l_i \leq r_i \leq |s|).

Output

For every query output one integer: the value of Ж-function of the corresponding substring.

Sample Input 1

abbd
4
2 3
1 3
3 3
1 4

Sample Output 1

3
3
1
4

Sample Input 2

bbaaa
5
2 4
1 5
1 5
3 3
1 2

Sample Output 2

3
6
6
1
3

Note

In the first sample case there are four queries:

  • the first query corresponds to the substring bb, and its Ж-function equals 2+1=32 + 1 = 3;
  • the second query corresponds to the substring abb, and its Ж-function equals 3+0+0=33 + 0 + 0 = 3;
  • the third query corresponds to the substring b, and its Ж-function equals 11.
  • the fourth query corresponds to the substring abdd, and its Ж-function equals 4+0+0+0=44 + 0 + 0 + 0= 4.