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.
from errno import ERANGE
from operator import truediv

from pyrsistent import InvariantException


def solve():
    s = input()
    q=int(input())
    l = int(len(s))
    dp = [[False for j in range(l+1)] for i in range(l+1)]
    dp2 = [[0 for j in range(l+1)] for i in range(l+1)]
    # for i in range(5):
    #     print(i)
    for i in range(l, 0, -1):
        for j in range(i+1, l+1):
            if s[i-1] == s[j-1]:
                if i+1 >= j-1 or dp[i+1][j-1]:
                    dp[i][j] = True
    for i in range(0, l+1):
        dp2[i][i] = 1
    for i in range(l, 0, -1):
        for j in range(i+1, l+1):
            if dp[i][j]:
                dp2[i][j] += 1
            dp2[i][j] = dp2[i][j]+(dp2[i+1][j] if i+1 <= j else 0)+(dp2[i]
                                                                    [j-1] if j-1 >= i else 0)-(dp2[i+1][j-1] if i+1 <= j-1 else 0)
    for i in range(q):
        l,r=map(int,input().split(" "))
        print(dp2[l][r])


# t = int(input())
# for i in range(t):
solve()
