#include<stdio.h>
#include<string.h>
int main()
{
int n;
while(scanf("%d",&n)!=EOF){
while(n--){
int min=3000000;
char s[1001];
scanf("%s",s);
for(int i=0;i<26;i++){
int sum=0;
char str[1001];
memset(str,'a'+i,sizeof(str));
for(int j=0;s[j]!=0;j++){
if(s[j]>str[j]){
if((s[j]-str[j])>('z'-s[j]+str[j]-'a'+1)) sum+='z'-s[j]+str[j]-'a'+1;
else sum+=s[j]-str[j];
}
else {
if((str[j]-s[j])>('z'-str[j]+s[j]-'a'+1)) sum+='z'-str[j]+s[j]-'a'+1;
else sum+=str[j]-s[j];
}
}
if(sum<=min) min=sum;
}
printf("%d\n",min);
}
}
return 0;
}
#include<string.h>
int main()
{
int n;
while(scanf("%d",&n)!=EOF){
while(n--){
int min=3000000;
char s[1001];
scanf("%s",s);
for(int i=0;i<26;i++){
int sum=0;
char str[1001];
memset(str,'a'+i,sizeof(str));
for(int j=0;s[j]!=0;j++){
if(s[j]>str[j]){
if((s[j]-str[j])>('z'-s[j]+str[j]-'a'+1)) sum+='z'-s[j]+str[j]-'a'+1;
else sum+=s[j]-str[j];
}
else {
if((str[j]-s[j])>('z'-str[j]+s[j]-'a'+1)) sum+='z'-str[j]+s[j]-'a'+1;
else sum+=str[j]-s[j];
}
}
if(sum<=min) min=sum;
}
printf("%d\n",min);
}
}
return 0;
}