


def countWords(s):
    s = s.replace('aaaaaaaaaa', 'a')
    s = s.replace('bbbbbbbbbb', 'b')
    s = s.replace('cccccccccc', 'c')
    words = 1
    while 'aa' in s:
        s = s.replace('aa', 'a')

    while 'bb' in s:
        s = s.replace('bb', 'b')

    while 'cc' in s:
        s = s.replace('cc', 'c')

    s = s.replace('abc', 'ac')

    #s = n
    lvls = {
        'a': 0,
        'b': 1,
        'c': 2,
    }
    lvl = 0
    for ch in s:
        if lvls[ch] < lvl:
            words += 1 
        lvl = lvls[ch]

    return words


s = input()


d = int(input())
q = []
ans = []
for a in range(d):
    q = list(map(lambda x: int(x) - 1, input().split(' ')))

    ans.append(countWords(s[q[0]: q[1] + 1]))

for a in ans:
    print(a)