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 bisect


def getPossible(word):
    dp = [ [ 0 for _ in range(len(word) + 1)] for _ in range(len(word) + 1) ]

    for col in range(len(word)):
        c = col + 1
        row = 0
        while c <= len(word) and row < len(word):
            if row == c:
                dp[row][c] = 1
                continue

            if word[row] == word[c - 1]:
                dp[row][c] = dp[row + 1][c] + 1
            else:
                dp[row][c] = max(dp[row + 1][c], dp[row][c - 1])
            c += 1
            row += 1

    return dp

def makePrefixSum(arr):
    a = [0]
    for n in range(1, len(arr)):
        a.append(a[-1] + arr[n])
    return a


def solution():
    word = input()
    q = int(input())
    dp = getPossible(word)
    for row in range(len(dp)):
        dp[row] = makePrefixSum(dp[row])

    for _ in range(q):
        l, r = map(int, input().split())
        l -= 1
        a = dp[l][r] - dp[l][l]
        print(a)
    

if __name__ == '__main__':
    solution()
        