using System;

class Solution {
  public static void Main(string[] args) {
      int min(string row, int lb, int rb)
            {
                int am = 0;
                string news, newstr="v";
                news = row.Substring(lb-1, rb-lb+1);
                int ii = 0;
                char th = ' ';
                while(ii< news.Length)
                {
                    if (news[ii] == th)
                    {
                        ii++;
                    }
                    else
                    {
                        newstr += news[ii];
                        if (newstr[newstr.Length - 1] < newstr[newstr.Length - 2]) am++;
                        th = news[ii];
                    }
                }
                return am-1;
            }
            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);
            }
  }
}
