s=input()
sc=[0]*(len(s)+1)
k=0
for i in range(len(s)-1):
        if s[i]>s[i+1]:k+=1
        sc[i+1]=k
q=int(input())
for i in range(q):
    sq=input().split()
    print(sc[int(sq[1])-1]-sc[int(sq[0])-1]+1)