import asyncio
from contextlib import suppress


async def safe(i):
    k = 0
    for j in range(i[0] - 1, i[1] - 1):
        if word[j] > word[j + 1]: k += 1
    print(k + 1)


async def main():
    global word
    word, data = input(), []
    for i in range(int(input())): data.append(list(map(int, input().split())))
    data.append([0,0])
    for i in data: task = asyncio.Task(safe(i))



    task.cancel()
    with suppress(asyncio.CancelledError):
        await task


loop = asyncio.new_event_loop()
asyncio.set_event_loop(loop)
try:
    loop.run_until_complete(main())
finally:
    loop.run_until_complete(loop.shutdown_asyncgens())
    loop.close()