using System;

class Solution {
  public static void Main(string[] args) {
      int min(string row, int lb, int rb)
            {
                int am = 0;
                for (int i = lb; i < rb; i++)
                {
                    if (row[i] < row[i - 1]) am++;
                }
                return am;
            }
            string s = Console.ReadLine();
            int n = int.Parse(Console.ReadLine());
            int[] minins = new int[n];
            for(int i = 0; i < n; i++)
            {
                string[] s1 = Console.ReadLine().Split(' ');
                int l = int.Parse(s1[0]);
                int r = int.Parse(s1[1]);
                minins[i] = min(s, l, r);
            }
            for (int i = 0; i < n; i++)
            {
                Console.WriteLine(minins[i]+1);
            }
            Console.ReadKey();
  }
}
