// Author: Adrian Kuegel
// Date: November 4th
// Algorithm: Greedy
// Complexity: O(n^2)

import java.io.*;
import java.util.*;

public class adrian {
	public static void main(String [] args) throws Exception {
		BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
		int tc = Integer.parseInt(in.readLine());
		int [] dist = new int[26];
		for (int i=0; i<13; ++i)
			dist[i] = i;
		for (int i=13; i<26; ++i)
			dist[i] = 26-i;
		while(tc-- > 0) {
			String name = in.readLine();
			int n = name.length();
			int best = 1000000000;
			for (int k=0; k<2; ++k) {
				int sumdiff = 0;
				for (int s=0; s<n; ++s) {
					sumdiff += dist[name.charAt(s)-'A'];
					int sumdiff2 = sumdiff;
					int cost = sumdiff + s;
					for (int i=n-1; i>s; --i) {
						if (name.charAt(i) != 'A') {
							sumdiff2 += dist[name.charAt(i)-'A'];
							cost = sumdiff2 + 2*s + n - i;
						}
					}
					best = Math.min(best, cost);
				}
				if (sumdiff == 0)
					best = 0;
				StringBuffer temp = new StringBuffer(name.substring(1));
				name = name.charAt(0) + (temp.reverse()).toString();
			}
			System.out.println(best);
		}
	}
}
