def permute_string(str): if len(str) == 0: return [''] prev_list = permute_string(str[1:len(str)]) next_list = [] for i in range(0,len(prev_list)): for j in range(0,len(str)): new_str = prev_list[i][0:j]+str[0]+prev_list[i][j:len(str)-1] if new_str not in next_list: next_list.append(new_str) return next_list s = input() n = int(input()) for n0 in range(n): arr = [int(v) for v in input().strip().split()] sTemp = s[arr[0]-1:arr[1]] lenSTemp = arr[1] - arr[0] + 1 setSTemp = set(sTemp) if len(setSTemp) == lenSTemp: print(0) elif lenSTemp%2 == 0: s1Temp = "" lenS1Temp = 0 for i in setSTemp: tempCount = sTemp.count(i) if tempCount%2 == 0: s1Temp += i*tempCount lenS1Temp += tempCount else: s1Temp += i*(tempCount-1) lenS1Temp += tempCount-1 s1TempHalf = "" setS1Temp = set(s1Temp) for i in setS1Temp: s1TempHalf += i*int(s1Temp.count(i)/2) print(len(permute_string(s1TempHalf)))