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;
        long[] m = new long[20001];
        String[] line = n.split("");
        char j = 'd';
        //int q = sc.nextInt();
        int k = 0;
        for (int i = 0; i < line.length; i++) {
            if (line[i].charAt(0)>=j){
                m[i] = k;
            }else{
                k++;
                m[i]=k;
            }
            j=line[i].charAt(0);
        }
        k = sc.nextInt();
        for (int i = 0; i < k; i++) {
            int l = sc.nextInt();
            int r = sc.nextInt();
            System.out.println(m[r-1]-m[l-1]+1);
        }
    }
}




















