s = input()
s=s.replace("a","1")
s=s.replace("b","2")
s=s.replace("c","3")
s=s.split()
q=int(input())
a=[]
for i in range (q):
    l=[int(b) for b in input().split()]
    a.append(l)
for i in range (0, q):
    j=0
    for k in range (a[i][0], a[i][1]+1):
        if s[k-1]!=s[k] or s[k-1]-s[k]!=1:
            j+=1
    print(j)

