while True:
    try:
        num = int(input())
        pld = range(26,0,-1)
        for i in range(num):
            name = input()
            chars = list(dict.fromkeys(name))
            counts = [ name.count(i) for i in chars ]
            counts.sort(reverse=True)
            max_pld = [ pld[j]*counts[j] for j in range(len(counts))]
            print(sum(max_pld))
    except:
        break