str1 = input()
n = int(input())
rez = []
for i in range(0,n,1):
    first,last = map(int,input().split())
    str2 = str1[first-1:last]
    str2 = str2.replace('a','1')
    str2 = str2.replace('b','2')
    str2 = str2.replace('c','3')
    str2 = str2.replace('32','3 2')
    str2 = str2.replace('31','3 1')
    str2 = str2.replace('21','2 1')
    rez.append(str(str2.count(' ')+1))
print('\n'.join(rez))