s = str(input())
q = int(input())
arr = []
abc = "abc"
str_numbers = ""

#print(s)

for n in s:
    str_numbers += str(abc.find(n))
#print(str_numbers)

for _ in range(q):
    arr.append(list(map(int, input().split())))

for i in arr:
    s = str_numbers[i[0] - 1:i[1]]
    count = 1
    s0 = s[0]
    for j in s:
        if j < s0:
            count += 1
        s0 = j
    print(count)

#print(*arr)
#print(s)
#print(q)