import java.util.Scanner;

public class Language {
    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();

            int numberOfWords = countWords(sentence, start - 1, end - 1);
            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;
    }
}
