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.
class PalindromeQuery():
    def __init__(self, s):
        self.s = s
        self.n = len(self.s)
        self.dp = [[0 for i in range(self.n)] for j in range(self.n)]
        self.num_pal = [[0 for i in range(self.n)] for j in range(self.n)]

    def populate(self):
        for l in range(1,self.n+1):
            for i in range(0, self.n-l+1):
                j = i+l-1
                if i == j:
                    self.num_pal[i][j] = 1
                elif i == j-1:
                    self.num_pal[i][j] = int(self.s[i] == self.s[j])
                else:
                    self.num_pal[i][j] = int(self.s[i] == self.s[j] and self.num_pal[i+1][j-1] > 0)


        for l in range(1, self.n+1):
            for i in range(0, self.n-l+1):
                j = i+l-1
                if i < j:
                    self.num_pal[i][j] = self.num_pal[i][j] + self.num_pal[i][j-1]


        for l in range(1, self.n+1):
            for i in range(0, self.n-l+1):
                j = i+l-1
                if i == j:
                    self.dp[i][j] = self.num_pal[i][j]
                else:
                    self.dp[i][j] = self.num_pal[i][j] + self.dp[i+1][j]

    def get(self, l, r):
        return self.dp[l][r]

s = input().strip(' ')
pq = PalindromeQuery(s)
pq.populate()
q = int(input())
for i in range(q):
    l,r = list(map(int,input().strip(' ').split(' ')))
    l,r = l-1,r-1
    print(pq.get(l,r))