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)
    l = 0
    r = len(lis) 
    print(answer)
    answer = 0