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