#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;
// 1. 先把每个字符出现的次数放在vector数组中
// 2. 对vector数组进行排序
// 3. 遍历数组,同时累加每个字符的漂亮权重,计算漂亮度
int main() {
    int n;
    cin >> n;
    string str;
    for(int i = 0; i < n; i++){
        cin >> str;
        vector<int> cnt(26, 0);
        for(auto s : str){
            cnt[s - 'a'] += 1;
        }

        sort(cnt.begin(), cnt.end());
        int start = 1;
        int beauty = 0;
        for(auto n : cnt){
            beauty += start*n;
            start++;
        }
        cout << beauty << endl;
    }
}
// 64 位输出请用 printf("%lld")