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.
'''
Created on 27-08-2019

@author: Martin
'''

s = input()
n = len(s)
tab = [[0 for i in range(n)] for j in range(n)]

for i in range(n):
    tab[i][i] = 1
    k = 1
    while((i-k>=0) & (i+k<n)):
        if(s[i-k] == s[i+k]):
            tab[i-k][i+k] = 1
            k += 1
        else:
            break

for i in range(n):
    k = 1
    while((i-k+1>=0) & (i+k<n)):
        if(s[i-k+1] == s[i+k]):
            tab[i-k+1][i+k] = 1
            k += 1
        else:
            break
        
acc = [[0 for i in range(n)] for j in range(n)]

acc[0][0] = tab[0][0]
for i in range(1,n):
    acc[0][i] = acc[0][i-1] + tab[0][i]

for i in range(1,n):
    acc[i][0] = acc[i-1][0] + tab[i][0]
    for j in range(1,n):
        acc[i][j] = acc[i-1][j] + acc[i][j-1] - acc[i-1][j-1] + tab[i][j]
        
q = int(input())

for i in range(q):
    a, b = map(int, input().split())
    if(b == 1):
        print(1)
    elif(a == 1):
        print(acc[b-1][b-1])
    else:
        print(acc[b-1][b-1] - acc[b-1][a-2] - acc[a-2][b-1] + acc[a-2][a-2])

    