#include<stdio.h>
int main
int number;
scanf("%d\n", &number);
for(int i=0; i<number; i++){
int hash[26]={0};
char name;
scanf("%c", &name);
while(name != '\n')
{
if(name>='a')
{
hash[name-'a']++;
}
else
{
hash[name-'A']++;
}
scanf("%c", &name);
}
int scord = 26;
int max = 0;
int all =0;
int ans = 0;
int loc = 0;
for(int i=0; i<26; i++)
{
if(hash[i] != 0)all++;
}
for(int i=0; i<all; i++)
{
for(int j=0; j<26; j++)
{
if(hash[j]>max){
max = hash[j];
loc = j;
}
}
ans+=scord*max;
max = 0;
hash[loc] = 0;
scord--;
}
printf("%d\n", ans);
}
}