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.
s=str(input())
dp=[[0 for i in range(len(s))]for j in range(len(s))]
for i in range(len(s)):
    dp[i][i]=1
n=len(s)    
for i in range(n-1):
    if s[i]==s[i+1]:
        dp[i][i+1]=1
        
# print(dp)       
for i in range(2,n):
    for j in range(n-i):
        # print(i,
        # j)
        if s[j]==s[i+j] and dp[j+1][i+j-1]:
            dp[j][i+j]=1
        
# a=[]
# for j in range(n):
#     c=0
#     for i in range(n):
#         c+=dp[i][j]
        
#     a.append(c)
    
# prefix=[[0 for i in range(n)]for j in range(n)]
for i in range(1,n):
    dp[0][i]=dp[0][i-1]+dp[0][i]
for i in range(1,n):
    dp[i][0]=dp[i-1][0]+dp[i][0]
    
for i in range(1,n):
    for j in range(1,n):
        dp[i][j]=dp[i][j]+dp[i-1][j]+dp[i][j-1]-dp[i-1][j-1]
# print(dp)
    
for _ in range(int(input())):
    a,b=map(int,input().split())
    a-=1
    b-=1
    # print(u,v)
    # print(u,v)
    # print(u-1)
    x=dp[b][b] 
    # print(a-1)
    # print(dp[b][b] ,dp[a-1][b] , dp[b][a-1] , dp[a-1][a-1])
    if a-1 >=0:
        x-=dp[a-1][b]
    if a-1>=0:
        x-=dp[b][a-1]
        
    if a-1>=0 :
        x+=dp[a-1][a-1]
    print(x)