def readList():
    return [int(i) for i in input().split()]

s = input()
prefSum = [0] * len(s)
ind = 0
cur = 1
prefSum[0] = 1
while ind + 1 < len(s):
	if s[ind] > s[ind + 1]:
		cur += 1
	prefSum[ind + 1] = cur
	ind += 1
q = int(input())
for qq in range(q):
	l, r = readList()
	if l == r:
		print(1)
		continue
	l -= 1
	r -= 1
	if l > 0:
		print(prefSum[r] - prefSum[l - 1])
	else:
		print(prefSum[r])