#!/bin/python3 import sys def initialize(s): a=s def answerQuery(s,l, r): flag=0 count=0 c=[] a=s[l-1:r] for i in range(0,len(a)): for j in range(i+1,len(a)): if(a[i]==a[j]): flag=1 if(a[i] not in c): c.append(a[i]) count+=1 if(count<(len(a)-(count+1))): count=len(a)-count-1 if(flag==1): return count else: return flag if __name__ == "__main__": s = input().strip() initialize(s) q = int(input().strip()) for a0 in range(q): l, r = input().strip().split(' ') l, r = [int(l), int(r)] result = answerQuery(s,l, r) print(result)