program Z1;

{$APPTYPE CONSOLE}

{%File 'output.txt'}
{%File 'input.txt'}

uses
  SysUtils;

var
  j:Char;
  s:AnsiString;
  i,k,st,fin:LongInt;
  m:array [0..20000] of LongInt;
begin
  Assign (Input,'input.txt');
  Reset (Input);
  Assign (Output,'output.txt');
  Rewrite (Output);
  read (s);
  m[0]:=0;
  j:='d';
  for i:=1 to Length(s) do
    begin
      if s[i]>=j then
        m[i]:=k
      else
        begin
          k:=k+1;
          m[i]:=k;
        end;
      j:=s[i];
      //write (m[i]);
    end;
  read (k);
  for i:=1 to k do
    begin
      Read (st,fin);
      Writeln (m[fin]-m[st]+1);
    end;
  Close (Input);
  Close (Output);
  { TODO -oUser -cConsole Main : Insert code here }
end.
