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);
For i:=1 to k-1 do
begin
j:=i;
g:=1;
Repeat
if (s[j+1]>s[j]) or (s[j+1]=s[j]) then
begin
g:=g+1;
j:=j+1;
end;
Until s[j+1]<s[j];
a[i]:=g;
end;
a[k]:=1;
For i:=1 to n do
begin
h:=0;
Read(l,r);
repeat
l:=l+a[l];
h:=h+1;
until l>r;
Writeln(h);
end;
end.
