var q, i, j, k:longint;
    l, r:array of longint;
    s:ansistring;
    p:array of boolean;


begin
  read(s);
  read(q);

  setlength(l, q);
  setlength(r, q);
  setlength(p, length(s));
  
  for i:=1 to q do read(l[i], r[i]);

  for i:=1 to length(s) do 
    if s[i]<s[i-1] then p[i]:=true;
  
  for i:=1 to q do 
    begin
      k:=0;
      for j:=l[i]+1 to r[i] do 
        if p[j] then inc(k);
      writeln(k+1);
    end;

end.
