def A(w):
    c=0
    for i in range(len(w)):
        if i==len(w)-1:
            c+=0
        elif int(w[i])>int(w[i+1]):
            c+=1
    return c+1
s=input()
s=s.replace("a","1")
s=s.replace("b","2")
s=s.replace("c","3")
count=int(input())
for i in range(count):
    a,b=map(int,input().split())
    print(A(s[a-1:b]))