import java.io.*; import java.util.*; import java.text.*; import java.math.*; import java.util.regex.*; public class Solution { static void initialize(String s) { // This function is called once before all queries. String s1=s; } static int answerQuery(String s,int l, int r) { s=s.substring(l-1,r-1); TreeMap m = new TreeMap<>(); int n = s.length(); int[][] R = new int[2][n+1]; s = "@" + s + "#"; for (int j = 0; j <= 1; j++) { int rp = 0; R[j][0] = 0; int i = 1; while (i <= n) { while (s.charAt(i - rp - 1) == s.charAt(i + j + rp)) rp++; 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); i += k; } } s = s.substring(1, s.length()-1); 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--) m.put(s.substring(i - rp - 1, i - rp - 1 + 2 * rp + j), 1); m.put(s.substring(i, i + 1), 1); } int max=0; for (Map.Entry ii:m.entrySet()){ String r2 = ii.getKey(); if(r2.length()>max) { max=r2.length(); } } int re=0; for (Map.Entry ii:m.entrySet()){ String r1 = ii.getKey(); if(r1.length()==max) { re++; } } re=re%((int)Math.pow(10,9)+7); return re; } 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(); } }