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

int moves(char c)
{
    return min(c - 'A', 'Z' - c + 1);
}

int main()
{
    int t; string n;
    cin >> t;

    while (t--) {
        cin >> n;

        int char_moves = 0;
        for (size_t ii = 0; ii < n.size(); ++ii)
            char_moves += moves(n[ii]);

        int pos_moves = 999999999;
        for (size_t ii = 0, jj; ii < n.size(); ++ii) {
            for (jj = ii+1; jj < n.size() && n[jj] == 'A'; ++jj);
            pos_moves = min(pos_moves,
                            (int) min(2*ii + n.size()-jj, 2*(n.size()-jj) + ii));
        }

        cout << char_moves + pos_moves << endl;
    }

    return 0;
}
