import java.util.*;
/**
* HJ45 名字的漂亮度 - 中等
*/
public class HJ045 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
while (sc.hasNext()) {
int count = Integer.parseInt(sc.nextLine());
for (int i = 0; i < count; i++) {
int score = getScore2(sc.nextLine());
System.out.println(score);
}
}
sc.close();
}
public static int getScore(String name) {
int score = 0;
char[] chars = name.toCharArray();
int[] count = new int[26];
for (char ch : chars) {
count[Character.toLowerCase(ch) - 'a']++;//统计每个字母出现的次数,忽略大小写
}
Arrays.sort(count);//升序排列
for (int i = 1; i <= 26; i++) {//计算漂亮度
score += i * count[i - 1];
}
return score;
}
public static int getScore2(String name) {
int score = 0;
char[] chars = name.toLowerCase().toCharArray();
Map<Character, Integer> map = new HashMap<>();
for (char ch : chars) {
map.put(ch, map.getOrDefault(ch, 0) + 1);
}
List<Integer> list = new ArrayList<>();
for (Integer value : map.values()) {
list.add(value);
}
Collections.sort(list);
for (int i = list.size(); i > 0; i--) {
score = score + list.get(i - 1) * (26 - list.size() + i);
}
return score;
}
}