s = input()
a = ""
q = int(input())
for x in range(q):
    l, r = map(int, input().split())
    o = s[l-1:r]
    p = "a"  
    slova = 1
    for y in o:
        if ord(p) > ord(y):
            slova+=1
        p=y
    print(slova)  
