def word(s1):
    k=1
    for i in range(len(s1)-1):
        if (ord(s1[i])>ord(s1[i+1])):
            k+=1
    print(k)

s=input()
q=int(input())
for i in range(q):
    sq=input().split()
    ss=s[int(sq[0])-1:int(sq[1]):]
    word(ss)