s = input()
q = int(input())
for _ in range(q):
    l, r = map(int, input().split())
    m = s[l-1:r]
    if len(m)==1:
        print(1)
    else:
        result = 1
        for i in range(1, r - l + 1):
            if m[i]<m[i-1]:
                result+=1
        print(result)