#!/bin/python3 import sys def initialize(s,l,r): # This function is called once before all queries. arr = [] for num in range(r-l+1): arr.append(s[l+num-1]) arr1 = list(set(arr)) arr.sort() arr1.sort() buffer = 0 extra = [] if len(arr1)!=len(arr): for z in range (len(arr1)): if arr1[z]!=arr[z+buffer]: while (arr1[z]!=arr[z+buffer]): buffer +=1 extra.append(arr[z+buffer-1]) numAvailable = len(arr1)-len(extra) if len(arr1)==len(extra): numAvailable=len(extra)*2 return(numAvailable*len(extra)) #def answerQuery(l, r): # Return the answer for this query modulo 1000000007. if __name__ == "__main__": s = input().strip() q = int(input().strip()) for a0 in range(q): l, r = input().strip().split(' ') l, r = [int(l), int(r)] result = initialize(s, l, r) print(result)