s = list(map(str, input()))
for i in range(len(s)):
    s[i] = ord(s[i])  - ord('a') + 1
r = []
for i in range(1, len(s)):
    if s[i] < s[i - 1]:
        r.append(i)
q = int(input())
for i in range(q):
    k = 1
    a, b = map(int, input().split())
    for el in r:
        if el >= a and el + 1 <= b:
            k += 1
    print(k)
