import java.util.Arrays;
import java.util.Scanner;
/**
* Created by cq on 2015/8/17.
*/
public class Main {
public static void main(String[] args){
Scanner in = new Scanner(System.in);
int nameNum = in.nextInt();
in.nextLine();
for (int i=0; i<nameNum; i++){
String s = in.nextLine();
System.out.println(beautyScore(s));
}
in.close();
}
public static int beautyScore(String name){
name = name.toLowerCase();
int len = name.length(), score = 0;
int[] records = new int[26];
for(int i=0; i<len; i++){
char alp = name.charAt(i);
records[alp-'a']++;
}
Arrays.sort(records);
int index = 25;
while (records[index] != 0){
score += (records[index]*(index+1));
index--;
}
return score;
}
}