You've got a string *s* = *s*<sub class="lower-index">1</sub>*s*<sub class="lower-index">2</sub>... *s*<sub class="lower-index">|*s*|</sub> of length |*s*|, consisting of lowercase English letters. There also are *q* queries, each query is described by two integers *l*<sub class="lower-index">*i*</sub>, *r*<sub class="lower-index">*i*</sub> (1 ≤ *l*<sub class="lower-index">*i*</sub> ≤ *r*<sub class="lower-index">*i*</sub> ≤ |*s*|). The answer to the query is the number of substrings of string *s*[*l*<sub class="lower-index">*i*</sub>... *r*<sub class="lower-index">*i*</sub>], which are palindromes.

String *s*[*l*... *r*] = *s*<sub class="lower-index">*l*</sub>*s*<sub class="lower-index">*l* + 1</sub>... *s*<sub class="lower-index">*r*</sub> (1 ≤ *l* ≤ *r* ≤ |*s*|) is a substring of string *s* = *s*<sub class="lower-index">1</sub>*s*<sub class="lower-index">2</sub>... *s*<sub class="lower-index">|*s*|</sub>.

String *t* is called a palindrome, if it reads the same from left to right and from right to left. Formally, if *t* = *t*<sub class="lower-index">1</sub>*t*<sub class="lower-index">2</sub>... *t*<sub class="lower-index">|*t*|</sub> = *t*<sub class="lower-index">|*t*|</sub>*t*<sub class="lower-index">|*t*| - 1</sub>... *t*<sub class="lower-index">1</sub>.
The first line contains string *s* (1 ≤ |*s*| ≤ 5000). The second line contains a single integer *q* (1 ≤ *q* ≤ 10<sup class="upper-index">6</sup>) — the number of queries. Next *q* lines contain the queries. The *i*-th of these lines contains two space-separated integers *l*<sub class="lower-index">*i*</sub>, *r*<sub class="lower-index">*i*</sub> (1 ≤ *l*<sub class="lower-index">*i*</sub> ≤ *r*<sub class="lower-index">*i*</sub> ≤ |*s*|) — the description of the *i*-th query.

It is guaranteed that the given string consists only of lowercase English letters.
import sys
import itertools



s =  sys.stdin.readline()
t = int(sys.stdin.readline())
n = len(s)
 
 
#now create the palindromic matrix
palindrome = [[0 for i in range(n+1)] for i in range(n+1)]
 
#for string of length 1 and 2 make it palindrome
 
for i in range(1,n+1):
    palindrome[i][i] = 1
    if i < n and s[i-1] == s[i]:
        palindrome[i][i+1] = 1
 
for ln in range(3,n+1):
    for i in range(1,n-ln+2):
        start = i
        end = i+ln-1
        if palindrome[start+1][end-1] and s[start-1] == s[end-1]:
            palindrome[start][end] = 1
 
for i in range(1,n+1):
    for j in range(1,n+1):
        palindrome[i][j] = palindrome[i-1][j]+palindrome[i][j-1]-palindrome[i-1][j-1]+palindrome[i][j]

CHUNK_LENGTH = t
lines_chunk = itertools.islice(sys.stdin, CHUNK_LENGTH)

ans = []
for each in lines_chunk:
    a,b = list(map(int,each.split()))
    term  =  palindrome[b][b] - palindrome[b][a-1] - palindrome[a-1][b]+palindrome[a-1][a-1]
    ans.append(term)

for each in ans:
    print(each)