import java.io.*; import java.util.*; import java.text.*; import java.math.*; import java.util.regex.*; public class Solution { static ArrayList al; static boolean checkPalindrome(String s) { // reverse the given String String reverse = new StringBuffer(s).reverse().toString(); // check whether the string is palindrome or not if (s.equals(reverse)) return true; else return false; } static void permute(String str, int l, int r) { if (l == r) al.add(str); else { for (int i = l; i <= r; i++) { str = swap(str,l,i); permute(str, l+1, r); str = swap(str,l,i); } } } static String swap(String a, int i, int j) { char temp; char[] charArray = a.toCharArray(); temp = charArray[i] ; charArray[i] = charArray[j]; charArray[j] = temp; return String.valueOf(charArray); } public static void main(String[] args) { Scanner scan = new Scanner(System.in); String str=scan.next(); int t=scan.nextInt(); while(t-->0) { int l=scan.nextInt(); int r=scan.nextInt(); al=new ArrayList<>(); String s= str.substring(l-1,r); permute(s, 0, s.length()-1); ArrayList ans=new ArrayList<>(); for(String x:al) { for(int i=0;ilen) len=X.length(); } HashMap hm=new HashMap<>(); for(String X:ans) { if(X.length()==len) { if(!hm.containsKey(X)) hm.put(X,1); } } int count=0; for(String X:hm.keySet()) { count++; } System.out.println(count); } } }