import java.io.*; import java.util.*; import java.text.*; import java.math.*; import java.util.regex.*; public class Solution { static int palindromeSubStrs(String s) { //map m; TreeMap m = new TreeMap<>(); int n = s.length(); // table for storing results (2 rows for odd- // and even-length palindromes int[][] R = new int[2][n+1]; // Find all sub-string palindromes from the // given input string insert 'guards' to // iterate easily over s s = "@" + s + "#"; int max = 0; for (int j = 0; j <= 1; j++) { int rp = 0; // length of 'palindrome radius' R[j][0] = 0; int i = 1; while (i <= n) { // Attempt to expand palindrome centered // at i while (s.charAt(i - rp - 1) == s.charAt(i + j + rp)) rp++; // Incrementing the length of // palindromic radius as and // when we find vaid palindrome if (rp > max) { max = rp; } // Assigning the found palindromic length // to odd/even length array R[j][i] = rp; int k = 1; while ((R[j][i - k] != rp - k) && (k < rp)) { R[j][i + k] = Math.min(R[j][i - k], rp - k); k++; } rp = Math.max(rp - k,0); if (rp > max) { max = rp; } i += k; } } // remove 'guards' s = s.substring(1, s.length()-1); // Put all obtained palindromes in a hash map to // find only distinct palindromess if (max == 2) { m.put(s.substring(0,1), 1); } for (int i = 1; i < n; i++) { for (int j = 0; j <= 1; j++) for (int rp = R[j][i]; rp > 0; rp--) if (s.substring(i - rp - 1, i - rp - 1 + 2 * rp + j).length() == max) { m.put(s.substring(i - rp - 1, i - rp - 1 + 2 * rp + j), 1); } if (max == 2) { m.put(s.substring(i, i + 1), 1); } } // printing all distinct palindromes from // hash map return m.size(); } static void initialize(String s) { // This function is called once before all queries. } static int answerQuery(String s, int l, int r) { // Return the answer for this query modulo 1000000007. return palindromeSubStrs(s.substring(l, r)); } public static void main(String[] args) { Scanner in = new Scanner(System.in); String s = in.next(); initialize(s); int q = in.nextInt(); for(int a0 = 0; a0 < q; a0++){ int l = in.nextInt(); int r = in.nextInt(); int result = answerQuery(s, l, r); System.out.println(result); } in.close(); } }