package voi22;

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[] amount = countWords(sentence);

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

            int a = amount[start];
            int b = amount[end];
            int numberOfWords = 1 + b - a;
            System.out.println(numberOfWords);
        }
    }

    public static int[] countWords(String sentence) {
        int[] amount = new int[sentence.length()];
        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++;
            }
            amount[i] = count;
        }
        return amount;
    }
}