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

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