//#define _GLIBCXX_DEBUG
//#include <atcoder/all>
#include <bits/stdc++.h>
#include <chrono>
#include <random>


using namespace std;
//using namespace atcoder;
#define rep(i,n) for (int i = 0;i < (int)(n);i++)
using ll = long long;
const ll MOD=1000000007;
//const ll MOD=998244353;
//using mint = modint998244353;
//using mint = modint1000000007;
const long long INF = 1LL << 60;
const double pi=acos(-1.0);
int dx[9] = {1, 0, -1, 0, 1, 1, -1, -1, 0};
int dy[9] = {0, 1, 0, -1, 1, -1, -1, 1, 0};

template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return true; } return false; }
template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return true; } return false; }



int main()
{
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  // cout << fixed << setprecision(15);

  string G="atcoder";
  ll T; cin>>T;
  rep(_,T){
    string S; cin>>S;
    ll N=S.size();
    auto MS=S;
    sort(MS.rbegin(),MS.rend());
    if(G>=MS) {cout<<-1<<'\n'; continue;}
    ll ans=INF;
    vector<ll> vec(7,-1);
    rep(i,N){
      rep(j,7){
        if(S[i]==G[j]&&vec[j]==-1) vec[j]=i;
      }
    }
    //for(auto v:vec) cout<<v<<'\n';
    rep(i,8){
      ll sans=0;
      rep(j,i){
        if(vec[j]==-1) goto OUT;
        sans+=abs(vec[j]-j);
      }
      int piyo=0;
      rep(j,N){
        int hoge=0;
        rep(k,i){
          if(j==vec[k]) hoge=1;
        }
        if(hoge) continue;
        if(S[j]>(i==7?'a'-1:G[i])){
          sans+=abs(j-i); 
          piyo=1;
          //cout<<i<<' '<<j<<'\n';
          break;
        }
      }
      //cout<<i<<' '<<sans<<'\n';
      if(piyo) chmin(ans,sans);
    }
    OUT:;
    
    cout<<ans<<'\n';
  }
  return 0;
}

/*
Author : Xinyuan
*/
#include <bits/stdc++.h>
using namespace std;
#define REP(i, n) for(int i = 0; i < n; i++)
#define all(x) (x).begin(), (x).end()
typedef pair<int, int> PII;
typedef vector<int> VI;
typedef vector<string> VS;
typedef vector<PII> VII;
typedef vector<VI> VVI;
typedef long long int  ll;
const int INF = 1e9 + 7;
const int mod = 1e9 + 7;
ll powmod(ll a,ll b) {ll res=1;a%=mod; assert(b>=0); for(;b;b>>=1){if(b&1)res=res*a%mod;a=a*a%mod;}return res;}
int nxt() {int x;scanf("%d", &x);return x;}


int main(){
    int w, x, y, z;
    cin >> w >> x >> y >> z;
    cout << w * z - x * y;
    return 0;
}

