s = input()
l1 = [1]
result = 1
lens = len(s)
for i in range(1, lens):
    if s[i]<s[i-1]:
        result+=1
        l1.append(i)
if l1[-1]!=lens:
    l1.append(lens)
q = int(input())
for _ in range(q):
    l, r = map(int, input().split())
    count = 0
    for i in range(result):
        if l<=l1[i]<l1[i+1]<=r:
            count+=1
    if l not in l1:
        count+=1
    if r not in l1:
        count+=1
    print(count)