import java.util.ArrayList;
import java.util.HashMap;
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<>();
        HashMap <Integer, Integer> name = new HashMap<>();
        name.put(1, 1);
        int n = 1;

        char l = mainLine.charAt(0);

        for (int i = 1; i<mainLine.length();i++){
            if ((int) mainLine.charAt(i) < (int) l ) n++;
            name.put(i+1,n);
            l = mainLine.charAt(i);
        }

        int q = scanner.nextInt();

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

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

            int a = name.get(f) - name.get(s) + 1;

            answer.add(a);

        }

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

    }
}
