from itertools import count


def countWords(s):
    words = 1
    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)