import sys
input = sys.stdin.readline

def bin_search(n):
    L, R = 0, len(x)
    while R - L > 1:
        mid = (L + R) // 2
        if x[mid] > n:
            R = mid
        else:
            L = mid
    return L

s = input()
if s[-1] == '\n':
    s = s[:-1]
x = [-1]
for i in range(1, len(s)):
    if s[i] < s[i - 1]:
        x.append(i + 1)
for q in range(int(input())):
    l, r = map(int, input().split())
    l += 1
    print(max(bin_search(r) - bin_search(l) + 1, 1))

