using System;

class Solution {
  public static void Main(string[] args) {
      int min(string row, int lb, int rb)
            {
                int am = 0;
                string news, newstr="";
                news = row.Substring(lb-1, rb-lb+1);
                newstr += news[0];
                int ii = 1;
                char[] ch;
                while(ii< news.Length)
                {
                    if (newstr[newstr.Length - 1] == 'a')
                    {
                        ch = new char[] { 'b', 'c' };
                    }
                    else if (newstr[newstr.Length - 1] == 'b')
                    {
                        ch = new char[] { 'a', 'c' };
                    }
                    else
                    {
                        ch = new char[] { 'a', 'b' };
                    }
                    if (news.IndexOfAny(ch) == -1||ii==-1) break;
                    else
                    {
                        newstr += news[news.IndexOfAny(ch)];
                        ii = news.IndexOfAny(ch);
                        news = news.Substring(ii);
                        ii = 1;
                    }
                }
                for (int i = 1; i < newstr.Length; i++)
                {
                    if (newstr[i] < newstr[i - 1]) am++;
                }
                //Console.WriteLine(am+1);
                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();
  }
}
