seq = input()
n = int(input())
req = []
for i in range(n):
    req.append(list(map(int, input().split())))

for i in range(n):
    prev = " "
    tmp_str = seq[req[i][0] - 1:req[i][1]]
    ans = 1
    for x in range(len(tmp_str)):
        if tmp_str[x] < prev: ans += 1
        prev = tmp_str[x]
    print(ans)
