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