s = input()
s1=""
n=int(input())
count=0
for i in range(n):
    a,b=map(int, input().split())
    s1=s[a-1:b:]
    print(s1)
    for j in range(len(s1)-1):
        if s1[j]<=s1[j+1]:
            continue
        else:
            count+=1
    if s1[len(s1)-1]<s1[len(s1)-2] or count==0:
        count+=1
    print(count)
    count=0
