#!/bin/python3 import sys def getMaxSub(l, r, S): #memo = abs(r-l+1) * [abs(r-l+1) * [0]] n = abs(r-l+1) memo = n * [n * [0]] isDistinct = n * [n * [0]] S = S[l:r+1] for i in range(n-1): memo[i][i] = 1 isDistinct[i][i] = 1 memo[i+1][i] = 0 memo[n-1][n-1] = 1 isDistinct[n-1][n-1] = 1 for j in range(n): for i in range(0, j): if S[i] == S[j]: memo[i][j] = 2 + memo[i+1][j-1] isDistinct[i][j] = 1 else: memo[i][j] = max(memo[i+1][j], memo[i][j-1]) maxLength = memo[l-1][r-1] r = 0 for j in range(n): for i in range(0, j): if memo[i][j] == maxLength and isDistinct[i][j] == 1: r += 1 return r def getNumWithLength(i, j, S, n): if abs (i - j) + 1 < n: return 0 if S[i] == S[j]: return 2 + getMaxSub(i+1, j-1, S) else: return max(getMaxSub(i+1, j, S), getMaxSub(i, j-1, S)) def initialize(s): # This function is called once before all queries. S = s def answerQuery(l, r, S): # Return the answer for this query modulo 1000000007. return getMaxSub(l-1, r-1, S) if __name__ == "__main__": s = input().strip() initialize(s) q = int(input().strip()) for a0 in range(q): l, r = input().strip().split(' ') l, r = [int(l), int(r)] result = answerQuery(l, r, s) print(result)