l = list(str(input()))
n = int(input())
la=list()
for i in range(len(l)):
    if ord(l[i])<ord(l[i-1]):
        la.append(i)
for i in range(n):
    a,b=map(int,input().split())
    ans=0
    for j in range(len(la)):
        if la[j]>a and la[j]<b:
            ans=ans+1
    print(ans+1)