program Project2;

{$APPTYPE CONSOLE}

uses
  SysUtils;
var n,m,i,j,k,g,l,r,h : LongInt;
s : AnsiString;
a : array [1..100000] of LongInt;
begin
Readln(s);
readln(n);
k:=Length(s);
g:=1;
fillchar(a,SizeOf(a),0);
For i:=1 to k do
begin
if s[i]<s[i-1] then
g:=g+1;
a[i]:=g;
end;
For i:=1 to n do
begin
read(l,r);
Writeln(a[r]-a[l]+1);
end;
end.
