import java.util.Scanner;


public class LanguageV2 {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);

        final String sentence = in.nextLine();

        int n = in.nextInt();
        for (int i = 0; i < n; i++) {
            int start = in.nextInt();
            int end = in.nextInt();

            String substring = sentence.substring(start - 1, end)
                            .replaceAll("a+", "a")
                            .replaceAll("b+", "b")
                            .replaceAll("c+", "c");
            int numberOfWords = countWords(substring);
            System.out.println(numberOfWords);
        }
    }

    public static int countWords(String sentence, int startIndex, int endIndex) {
        int count = 1;
        char[] chars = sentence.toCharArray();
        for (int i = startIndex; i <= endIndex; i++) {
            if (i != startIndex && chars[i] < chars[i - 1]) {
                count++;
            }
        }
        return count;
    }

    public static int countWords(String sentence) {
        int count = 1;
        char[] chars = sentence.toCharArray();
        for (int i = 0; i <= sentence.length() - 1; i++) {
            if (i != 0 && chars[i] < chars[i - 1]) {
                count++;
            }
        }
        return count;
    }
}
