import java.io.*; import java.util.*; import java.text.*; import java.math.*; import java.util.regex.*; public class Solution { static int fact(int x) { int i,f=1; for(i=2;i<=x;i++) f*=i; return f; } public static void main(String[] args) { Scanner in = new Scanner(System.in); String s = in.next(); char b; int q = in.nextInt(); for(int a0 = 0; a0 < q; a0++){ int l = in.nextInt(); int r = in.nextInt(); TreeSet a =new TreeSet(); for(int j=l;j<=r;j++) { b=s.charAt(j-1); a.add(b); } int y=a.size(); int z=(r-l+1); //System.out.println(y + " "+z); int s1; if((2*y)==z) s1=fact(y); else if(y