slo = list(input())
n = int(input())
spi = []
res = [[]]
t = 0
for i in range(n):
   a = list(map(int, input().split()))
   spi.append(a)

for i in range(len(slo)):
   if slo[i] == "a":
      slo[i] = 1
   elif slo[i] == "b":
      slo[i] = 2
   elif slo[i] == "c":
      slo[i] = 3

while t != (len(slo)-1):
  if slo[t] <= slo[t+1]:
    t=t+1
  elif slo[t] > slo[t+1]:
    slo[t+1] = -slo[t+1]
    t=t+1
x =1
for y in range(n):
  for r in range(spi[y][0]-1,spi[y][1]):
    if slo[r] < 0:
      x = x+1
    else:
      continue
  print(x)
  x = 1