s=input()
q=int(input())
words = []
ranges = []
for i in range(q):
    ss = input().split()
    l = int(ss[0])
    r = int(ss[1])
    words.append(1)
    ranges.append([l-1, r])

pc = ''
for pos in range(len(s)):
    c = s[pos]
    if c < pc:
        for idx in range(q):
            if ranges[idx][0] < pos < ranges[idx][1]:
                words[idx] = words[idx] + 1
    pc = c

for i in range(q):
    print(words[i])

