#include <iostream>
using namespace std;
int a;
string b;
int main() {
cin>>a;
while(a--){
cin>>b;
int c=b.size(),d=65,x,y,z,p=0;
for(int i=0;i<c;i++){
x=90-d+(b[i]-65)+1;
y=90-b[i]+(d-65)+1;
z=abs(b[i]-d);
if(y<=z&&y<=x)p+=y;
else if(x<=z&&x<=y)p+=x;
else if(z<=x&&z<=y)p+=z;
d=b[i];
}
cout<<p<<endl;
}
return 0;
}
SPOJ.com - Problem MWP4_3C