s = input()
n = int(input())
for i in range(n):
	a,b = input().split()
	a = int(a)
	b = int(b)
	cur = 1
	ts = s[a-1:b]
	#print(ts)
	for j in range(len(ts)-1):
		if ts[j+1] < ts[j]:
			cur+=1
	print(cur)