using System;

class Solution {
  public static void Main(string[] args) {
    string[] data = Console.ReadLine().Split(' ');
    string sentence = data[0];
    int q = Convert.ToInt32(Console.ReadLine());
    for (int i = 0; i < q; i++)
    {
        data = Console.ReadLine().Split(' ');
        int l = Convert.ToInt32(data[0]) - 1;
        int r = Convert.ToInt32(data[1]) - 1;
        int result = 1;

        char currentLetter = sentence[l];
        char previousLetter = sentence[l];
        for (int currentPosition = l; currentPosition <= r; currentPosition++)
        {
            currentLetter = sentence[currentPosition];
            if (!(Convert.ToInt32(currentLetter) >= Convert.ToInt32(previousLetter)))
            {
                result++;
            }
            previousLetter = currentLetter;
        }
        Console.WriteLine(result);
    }
  }
}
