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
p = 131
mod = 10**9+7
inv = 190839696

S = sys.stdin.readline()
L = len(S)

pows = [1] * (len(S)+1)
powsinv = [1] * 5005

for i in range(1, 5001):
    powsinv[i] = (powsinv[i-1] * inv)%mod
    
for i in range(1, len(S)):
    pows[i] = (pows[i-1] * p)%mod
    
prefx = [0] * len(S)

prefx[0] = ord(S[0])
for i in range(1, len(S)):
    prefx[i] = (prefx[i-1] + pows[i] * ord(S[i]))%mod
    
S = S[::-1]

prefx2 = [0] * len(S)

prefx2[0] = ord(S[0])
for i in range(1, len(S)):
    prefx2[i] = (prefx2[i-1] + pows[i] * ord(S[i]))%mod



def part(l, r, prefx):
    if l == 0:
        return prefx[r]
    return ((prefx[r] - prefx[l-1]) * powsinv[l])%mod

def palindrome(l, r):
    return part(l, r, prefx) == part(len(S)-r-1, len(S)-l-1, prefx2)

ans = [[0]*5005 for _ in range(5005)]
query = [[0]*5005 for _ in range(5005)]
for i in range(L):
    for j in range(i, L):
        if palindrome(i, j):
            ans[i][j] = 1

for g in range(L-1, -1, -1):
    counter = 0
    for y in range(g, L):
        counter += ans[g][y]
        query[g][y] = counter + query[g+1][y]

for i in range(int(input())):
    x, y = map(int, sys.stdin.readline().split(' '))
    print(query[x-1][y-1])