s = input()
k = len(s)
a = [0]*k
ans = 0
for i in range(0, k):
    if s[i] == 'a':
        a[i] = 1
    if s[i] == 'b':
        a[i] = 2
    if s[i] == 'c':
        a[i] = 3
q = int(input())
for i in range(0, q):
    l, r = map(int, input().split())
    for j in range(l-1, r-1):
        if a[j] > a[j+1]:
            ans = ans + 1
    print(ans+1)
    ans = 0

