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


begin
  read(s); n:=length(s);
  read(q);

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

  for i:=1 to n do 
    if s[i]<s[i-1] then p[i]:=true;
  
  for i:=1 to n do 
    if p[i] 
      then a[i]:=a[i-1]+1 
      else a[i]:=a[i-1];
    
  for i:=1 to q do 
    writeln(a[r[i]] - a[l[i]] + 1);

end.
