import sys;input=sys.stdin.readline
s=input()+'A'
pr=[0]*(len(s))
l=0
for i in range(1,len(s)):
    if s[i-1]>s[i]:
        pr[l]=pr[l-1]+1
        for j in range(l+1,i+1):
            pr[j]=pr[j-1]
        l=i
pr=[0]+pr[:-1]
for i in range(int(input())):
    l,r=map(int,input().split())
    l-=1
    print(pr[r]-pr[l]+(pr[l]==pr[l+1]))
        
