word, data = input(), []
for i in range(int(input())): data.append(list(map(int, input().split())))
for i in data:
    k = 1
    for s in range(i[0], i[1] - 1):
        if word[s] > word[s + 1]: k = k + 1
    print(k)