#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
using namespace std;
int main()
{
int test;
cin >> test;
while (test--){
string str;
cin >> str;
int i, k = 26, res = 0;
vector<int> vec(k, 0);
for (i = 0; i < str.length(); ++i){
if (str[i] >= 'a' && str[i] <= 'z')
vec[str[i] - 'a']++;
else
vec[str[i] - 'A']++;
}
sort(vec.begin(), vec.end());
for (i = 25; i >= 0; --i){
res += vec[i] * k--;
}
cout << res << endl;
}
return 0;
}