def bin_p(li, elem):
    l = -1
    r = len(li)
    while r > l + 1:
        mid = (r + l) // 2
        if li[mid] > elem:
            r = mid
        else:
            l = mid
    return l, r, li[l] == elem

s = input()
Prl = []
for i in range(len(s) - 1):
    if s[i] > s[i + 1]:
        Prl.append(i + 1)
Prl.append(len(s))

for _ in range(int(input())):
    x, y = map(int, input().split())
    otv = 0
    l_x, r_x, x_in = bin_p(Prl, x - 1)
    l_y, r_y, y_in = bin_p(Prl, y)
    pos_x = l_x
    if y_in:
        pos_y = l_y
    else:
        pos_y = r_y
    otv += pos_y - pos_x
    print(otv)