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