import java.util.Scanner;

public class Main {
    public static void main(String[] main) {
        Scanner scanner = new Scanner(System.in);
        String source = scanner.next();

        int count = scanner.nextInt();
        for(int i = 0; i < count; i++) {
            int l = scanner.nextInt() - 1;
            int r = scanner.nextInt();

            String split = source.substring(l, r);
            char prevChar = 0;
            int wordCount = 0;
            for(int j = 0; j < r - l; j++) {
                if(split.charAt(j) < prevChar && split.charAt(j) != prevChar) {
                    wordCount++;
                }
                prevChar = split.charAt(j);
            }
            System.out.println(wordCount + 1);
        }
    }
}