n = input()
q = int(input())
a = [0]*10**5
l,k=0,0
for i in range(0, q):
    l,k = input().split(' ')
    res = 0
    for k in range(int(l)-1, int(k)-1):
        if n[k]>n[k+1]:
            res+=1
    print(res+1)