s = input()
n = int(input())

def func(s):
    counter = 1
    for j in range(1, len(s)):
        if(ord(s[j]) < ord(s[j-1])):
            counter += 1
    return counter

for i in range(0, n):
    c = input().split(" ")
    print(func(s[int(c[0]):int(c[1])]))