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