s=input()
sc=[]
for i in range(len(s)-1):
        if s[i]>s[i+1]:
            sc.append(i+1)
q=int(input())
for i in range(q):
    sq=input().split()
    k=1
    for i in sc:
        if (int(sq[0])<= i <=int(sq[1])-1):k+=1
    print(k)