using System;

class Solution {
  public static void Main(string[] args) {
        string name = Console.ReadLine();
        int q = Convert.ToInt32(Console.ReadLine());
        while (q>0)
        {
            q--;
            int[] data = Array.ConvertAll(Console.ReadLine().Split(' '), int.Parse);
            int c = 0;
            int tmp = 0;
            int res = 1;
            for (int i = data[0] - 1; i < data[1]; i++) 
            {
                tmp = Convert.ToInt32(name[i]);
                if (c > tmp)
                {
                    res++;
                }
                c = Convert.ToInt32(name[i]);
            }
            Console.WriteLine(res);
        }
  }
}
