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