#include <iostream>
#include <map>
#include <vector>
#include <string>
#include <algorithm>
using namespace std;

/*
思路:出现字母最多到最少,对应的漂亮度从26到1。 将大写转化小写字母,字母-’a‘等到对应数字,对所有数字进行排序alpherbet,
alpherbet与1~26相乘求和。
*/
int BeautifulDegreeOfName(string  strIn)
{
    vector<int> alpherbet(26);
    int mostValue = 0;
    for(int j = 0; j < strIn.size(); j++) {
        if(strIn[j] >= 'A' && strIn[j] <= 'Z') {
            strIn[j] = strIn[j] + 32;
        }
    }
    
    for(int i = 0; i < strIn.size(); i++) {
        if(strIn[i] >= 'a' && strIn[i] <= 'z') {
            alpherbet[strIn[i] - 'a'] += 1;
        }
    }
    sort(alpherbet.begin(), alpherbet.end());
    
    for(int i = 0; i < 26; i++) {
        int j = i + 1;
        mostValue += alpherbet[i]*j;
    }
    return mostValue;
   
}
int main()
{
    int n;
    while(cin>>n) {
        for(int i = 0; i < n; i++) {
            string strIn;
            cin>>strIn;
            cout<<BeautifulDegreeOfName(strIn)<<endl;;
        }

    }
}