import java.io.*; import java.util.*; import java.text.*; import java.math.*; import java.util.regex.*; public class Solution { public static StringBuilder sourceString = new StringBuilder(); public static int modulo = 1000000007; static void initialize(String s) { sourceString.append(s); } static int answerQuery(int l, int r) { Map freqMap = new HashMap<>(); for(int i=l-1; i < r; i++){ char cur = sourceString.charAt(i); if(freqMap.containsKey(cur)){ freqMap.put(cur, freqMap.get(cur)+1); }else{ freqMap.put(cur, 1); } } SortedSet freqs = new TreeSet<>(); freqs.addAll(freqMap.values()); int min = Integer.MAX_VALUE; for(Integer freq: freqs){ if(freq/2 > 0 && freq < min){ min = freq; break; } } int quantity = freqs.tailSet(min); return min/2; } 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(l, r); System.out.println(result); } in.close(); } }