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 = input()

d1 = [0] * len(s)
def foo1() -> int:
    l = 0
    r = -1
    for i in range(len(s)):
        k = 1 if i > r else min(d1[l + r - i], r - i + 1)
        while (i + k < len(s)) and (i - k >= 0) and s[i + k] == s[i - k]:
            k += 1
        d1[i] = k
        if (i + k - 1) > r:
            l = i - k + 1
            r = i + k - 1
    return sum(d1)

d2 = [0] * len(s)
def foo2() -> int:
    l = 0
    r = -1
    for i in range(len(s)):
        k = 0 if i > r else min(d2[r - i + l + 1], r - i + 1)
        while (i + k < len(s)) and (i - k - 1 >= 0) and (s[i - k - 1] == s[i + k]):
            k += 1
            # print(i)
        d2[i] += k
        if (i + k - 1 > r):
            r = i + k - 1
            l = i - k
    return sum(d2)


foo1()
foo2()

res = []
sun = 0
for i in range(len(d1)):
    res.append(d1[i] + d2[i])

n = int(input())
for i in range(n):
    l, r = map(int, input().split())
    l -= 1
    r -= 1
    for j in range(l, r + 1):
        sun += min(d1[j], min(r - j + 1, j - l + 1))
        sun += min(d2[j], min(r - j + 1, j - l))
    print(sun)
    sun = 0


