s=raw_input() n=input() for _ in range(n): l,r=map(int,raw_input().split(" ")) item,item1={},{} for i in range(l-1,r): if s[i] in item: item[s[i]]=0 if s[i] in item1: item1[s[i]]+=1 else: item1[s[i]]=1 elif s[i] not in item: item[s[i]]=1 print sum(item1.values())+max(item.values())