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.
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
c {c} 1
ca {c, a} 2
caa {c, a, a, aa}  4
caaa {c, a, a, a, aa, aa} 6
caaab {c, a, a, a, aa, aa, b} 7
caaaba {c, a, a, a, b, a, aa, aa, aaa, aba} 10

c 1
c, a 2
c, a, a, aa 4
c, a, a, a, aa, aa, aaa  7

c, a, a, a, b, aa, aa, aaa 8

c, a, a, a, b, a, aa, aa, aaa, aba 10
"""




s = input()
n = len(s)
s = ' ' + s


dp = [[0] * (5001) for _ in range(5001)]
prefix_dp = [[0] * (5001) for _ in range(5001)]
for i in range(1, n + 1):
    dp[i][i] = 1
    
for i in range(1, n):
    if s[i] == s[i + 1]:
        dp[i][i + 1] = 1


for l in range(n - 2):
    for i in range(1, n - l):
        if dp[i][i + l] and s[i - 1] == s[i + l + 1]:
            dp[i - 1][i + l + 1] = 1


for i in range(1, n + 1):
    for j in range(1, n + 1):
        prefix_dp[i][j] = dp[i][j] + prefix_dp[i - 1][j] + prefix_dp[i][j - 1] - prefix_dp[i - 1][j - 1]
        


# # palindromeSolver.query(4, 6)
Q = int(input())
for _ in range(Q):
    i, j = map(int, input().split())
    print(prefix_dp[j][j] - prefix_dp[i - 1][j] - prefix_dp[j][i - 1] + prefix_dp[i - 1][i - 1])
