dictionary = {' ':0,'a':1,'b':2,'c':3}

a = input()
n = int(input())
list1 = []
for i in range(n):
    x,y = input().split(' ')
    list2 = [int(x),int(y)]
    list1.append(list2)

for i in range(n):
    b = a[list1[i][0]-1:list1[i][1]]
    i = 0
    while i < len(b)-1:
        if dictionary[b[i]] > dictionary[b[i+1]]:
            b = b[0:i+1] + ' ' + b[i+1:]
        i += 1
    b = b.split()
    print(len(b))
