a = input()
ans = 0
q = int(input())
for i in range(0, q):
    l, r = map(int, input().split())
    for j in range(l-1, r-1):
        if a[j] > a[j+1]:
            ans = ans + 1
    print(ans+1)
    ans = 0

