s = input()
k = len(s)
map_ = [0 for i in range(k)]
map_[0] = 1
for i in range(1, k):
    if s[i] == s[i-1] or s[i] > s[i-1]:
        map_[i] = map_[i-1]
    else:
        map_[i] = map_[i-1] + 1
q = int(input())
p = []
for i in range(q):
    l, r = map(int, input().split())
    print(map_[r-1]-map_[l-1]+1)