import java.util.ArrayList;
import java.util.Scanner;

public class Main {

    public static void main(String[] args) {

        Scanner scanner = new Scanner(System.in);
        String mainLine = scanner.nextLine();

        ArrayList<Integer> answer = new ArrayList<>();
        int q = scanner.nextInt();

        for (int r = 0; r < q; r++) {

            int s = scanner.nextInt();
            int f = scanner.nextInt();

            int a = 1;

            char l = mainLine.charAt(s-1);

            for (int i = s; i<f;i++){
                if ((int) mainLine.charAt(i) < (int) l )a++;
                l = mainLine.charAt(i);
            }

            answer.add(a);

        }

        for (int i : answer) System.out.println(i);

    }
}