import java.util.Arrays;
import java.util.Scanner;
import java.util.SortedMap;

public class Main {


    public static void main(String[] args) {
        new Main().run();
    }

    private void run() {
        Scanner sc = new Scanner(System.in);
        String n = sc.nextLine();
        int count =0;
        String[] line = n.split("");
        int q = sc.nextInt();
        for (int i = 0; i < q; i++) {
            int l = sc.nextInt();
            int r = sc.nextInt();
            for (int j = l-1; j < r-1; j++) {
               if(line[j].charAt(0) > line[j+1].charAt(0)) count++;

            }
            System.out.println(count+1);
            count=0;
        }
        
    }
}




















