a = str(input())
n = int(input())
rngs = []
ltrs = "abc"
b = ""
for i in range(n):
    rngs.append(list(map(int, input().split())))
for i in a:
    b += str(ltrs.find(i))

for i in rngs:
    a = b[i[0] - 1:i[1]]
    prev = a[0]
    c = 1
    for j in a:
        if int(j) < int(prev):
            c+=1
        prev = j
    print(c)
