Counting palindromes

Count the palindromic substrings fully contained in each query interval of a lowercase string.

Hard8String matchingSegment treeSortingMathNo attempts yetTime limit2sMemory limit64 MB

Problem

The organizers wanted to hold a practice contest but had nothing prepared, so they took the two words that came to mind, palindrome and data structure, and built a problem out of them.

Seunghyun made a string SS that uses only lowercase letters. For integers ii and jj with 1ijS1 \le i \le j \le |S|, let S[i..j]S[i..j] be the substring formed by joining the ii-th character of SS, the (i+1)(i+1)-th character, and so on up to the jj-th character.

Each query is a pair of integers aa and bb. For that query, count the palindromes that lie inside S[a..b]S[a..b]. In other words, count the pairs (x,y)(x, y) with axyba \le x \le y \le b such that S[x..y]S[x..y] is a palindrome. Two occurrences of the same substring at different starting positions count separately.

Input

The first line contains the string SS that Seunghyun made. SS uses only the lowercase letters a through z, and its length satisfies 1S1000001 \le |S| \le 100\,000.

The second line contains the number of queries QQ (1Q3000001 \le Q \le 300\,000). Each of the next QQ lines contains two integers aia_i and bib_i (1aibiS1 \le a_i \le b_i \le |S|).

Output

For each query, print the number of palindromes inside S[ai..bi]S[a_i..b_i] on its own line, in the order the queries are given.

Hint

A string T=t1t2t3tnT = t_1 t_2 t_3 \cdots t_n is a palindrome when reading it from the front gives the same string as reading it from the back, that is, when t1t2t3tn1tn=tntn1t3t2t1t_1 t_2 t_3 \cdots t_{n-1} t_n = t_n t_{n-1} \cdots t_3 t_2 t_1 holds. A string of length 1 is always a palindrome.

S|S| is the length of the string SS.