def slova(s):
    d = 'abc'
    l = 1
    for i in range(len(s)):
        if i==0 or s[i-1]<=s[i]:
            pass
        else:
            l +=1
                
    return l
s = ''
for i in input():
    if i=='a':
        s+='1'
    elif i=='b':
        s+='2'
    else:
        s+='3'
for i in range(int(input())):
    l, r = map(int, input().split())
    print(slova(s[int(l)-1: int(r)]))