def slova(s):
    n = 0
    l = 0
    for i in range(len(s)):
        if i==0 or int(s[i])>int(s[i-1]):
            n+=1
        else:
            l+=1
            n = 1
    if n!=0:
        l+=1
    return l
s = input()
a = ''
f = 'abc'
for i in range(len(s)):
    if i==0:
        a+=str(f.index(s[i])+1)
    elif s[i]==s[i-1]:
        a+='0'
    else:
        a+=str(f.index(s[i])+1)
f = []
for i in range(int(input())):
    f = []
    l, r = map(int, input().split())
    for i in a[int(l)-1: int(r)]:
        if i!='0':
            f.append(i)
    print(slova(f))