def slova(s):
    n = 0
    l = 0
    for i in range(len(s)):
        if i==0 or int(s[i])>int(s[i-1]):
            n+=1
        else:
            l+=1
            n = 1
    if n!=0:
        l+=1
    return l
s = input()
import sys
input = sys.stdin.readline
a = ''
f = 'abc'
g = {0:0}
d = 0
for i in range(len(s)):
    if i==0:
        a+=str(f.index(s[i])+1)
    elif s[i]==s[i-1]:
        d+=1
        g[i] = i-d
    else:
        a+=str(f.index(s[i])+1)
        g[i] = i-d
f = []
s = 0
for i in range(int(input())):
    f = []
    l, r = map(int, input().split())
    f = a[g[int(l)-1]: g[int(r)-1]+1]
    print(slova(f))
    