#include <bits/stdc++.h> using namespace::std;

int main() {

int num;
cin >> num;
while (num--) {
    int i = 26;
    int sum = 0;
    int cnt[26] = {0};
    string str;

    cin >> str;
    for (auto c : str) {
        c = tolower(c);
        ++cnt[c-'a'];
    }
    sort(cnt, cnt + 26, [](const int c1, const int c2) { return c1 > c2; });
    for (auto const &c : cnt) {
        sum += c * i--;
    }
    cout << sum << endl;
}

return 0;

}