s=input()
q=int(input())
for i in range (q):
    ss=input().split()
    l=int(ss[0])
    r=int(ss[1])

    words = 0
    prev = ''
    for c in s[l-1:r]:
        if c < prev:
            words = words + 1
        prev = c

    print(words + 1)
