s = input()
n=int(input())
count=0
for i in range(n):
    a,b=map(int, input().split())
    s=s[a-1:b:]
    for j in range(len(s)-1):
        if s[j]<=s[j+1]:
            continue
        else:
            count+=1
    count+=1
    print(count)
