/* NWERC 2010: Sample solution to "Highscore"
 * Author: Sean Lip
 *
 * Algorithm: 
 * 
 * Treat letter-change costs and movement costs separately. Brute force over possibilities for the latter.
 */

#include <iostream>
#include <cstring>
#include <algorithm>

using namespace std;

int main()
{
	int T;
	cin >> T;
	
	for(int i = 0; i < T; i++)
	{
		string b;
		cin >> b;
		string a(b.size(),'A');
		
		bool B[1001];
		memset(B,0,sizeof B);
		
		int ans = 0;
		for(int j = 0; j < a.size(); j++)
		if(a[j] < b[j]) ans += min(b[j]-a[j], a[j] + 26 -b[j]), B[j] = 1;
		else if(a[j] > b[j]) ans += min(a[j]-b[j], b[j]+26-a[j]), B[j] = 1;
		
		int addCost = a.size()-1;
		
		for(int j = 1; j < a.size(); j++)
		if(B[j] == 0 && (j == 1 || B[j-1] == 1))
		{
			int k = j;
			while(k < a.size() && B[k] == 0)
				k++;
			
			//ignore the segment with left endpoint j
			//so, need to cover 0..(j-1) and k..(a.size()-1)
			
			addCost = min(addCost, j-1 + j-1 + (int)a.size()-k);
			addCost = min(addCost, (int)a.size()-k + (int)a.size()-k + j-1);
		}
		
		cout << ans+addCost << endl;
	}
	return 0;
}
