/* package whatever; // don't place package name! */

import java.util.*;
import java.lang.*;
import java.io.*;

/* Name of the class has to be "Main" only if the class is public. */
class Ideone
{
	public static void main (String[] args) throws java.lang.Exception
	{
		// your code goes here
		Scanner sc = new Scanner(System.in);
		
		String s = sc.nextLine();
		Integer n = sc.nextInt(); 
		
		int[][] queries = new int[n][2];
		
		for(int i = 0; i < n; i++){
			queries[i][0] = sc.nextInt() - 1;
			queries[i][1] = sc.nextInt() - 1;
		}
		 
		Integer m = s.length();
		 
		int[][] dp = new int[m + 1][m + 1];
		int[][] ans = new int[m + 1][m + 1];
		
		for(int i = 0; i < m; i++){
			dp[i][i] = 1;
			ans[i][i] = 1;
		}
		
		for(int i = 0; i < m-1; i++){
			if(s.charAt(i) == s.charAt(i+1)){
				dp[i][i+1] = 1;
			}
				ans[i][i+1] = dp[i][i] + dp[i+1][i+1] + dp[i][i+1];

		}
		
		for(int len = 3; len <= m; len++){
			for(int i = 0; i <= m - len; i++){
				int j = i + len - 1;
				
				if(s.charAt(i) == s.charAt(j) && dp[i+1][j-1] == 1){
					dp[i][j] = 1;
				}
				
				ans[i][j] = ans[i][j-1] + ans[i+1][j] - ans[i+1][j-1] + dp[i][j]; 
			} 
		}
		
		for(int[] q : queries){
			int count = 0;
			for(int i = q[0]; i <= q[1]; i++){
			for(int j = i; j <= q[1]; j++){
			System.out.println(dp[i][j]);
			}
			}
			
		}
	}
}