s = input()
n = len(s)
a = ''
i = 0
while i < n:
    while i+1 < n and ord(s[i]) <= ord(s[i+1]):
        a += '0'
        i += 1
    a += '1'
    i += 1
t = int(input())
for _ in range(t):
    l, r = map(int, input().split())
    c = a[l-1:r].count('1')
    if a[r-1] != '1':
        c += 1
    print(c)