n = str(input())
t = int(input())
answer = 0
a = 0
b = 0
c = 0
lis = []
for i in range(1, len(n)):
    if n[i-1] > n[i]:
        lis.append(i)
lis.append(len(n))
for i in range(0, t):
    answer = 0
    a,b = input().split()
    a = int(a)
    b = int(b)
    for j in lis:
        if j >= a and j <= b:
            answer += 1
        if j == b:
            c = 1
        if j > b:
            break
    if c == 0:
        answer += 1
    c = 0
        
    print(answer)