#include <bits/stdc++.h>
using namespace std;

typedef int_fast32_t int32;
typedef int_fast64_t int64;

const int32 inf = 1e9+7;
const int32 MOD = 1000000007;
const int64 llinf = 1e18;

#define YES(n) cout << ((n) ? "YES\n" : "NO\n"  )
#define Yes(n) cout << ((n) ? "Yes\n" : "No\n"  )
#define POSSIBLE(n) cout << ((n) ? "POSSIBLE\n" : "IMPOSSIBLE\n"  )
#define ANS(n) cout << (n) << "\n"
#define REP(i,n) for(int64 i=0;i<(n);++i)
#define FOR(i,a,b) for(int64 i=(a);i<(b);i++)
#define FORR(i,a,b) for(int64 i=(a);i>=(b);i--)
#define all(obj) (obj).begin(),(obj).end()
#define rall(obj) (obj).rbegin(),(obj).rend()
#define fi first
#define se second
#define pb(a) push_back(a)
typedef pair<int32,int32> pii;
typedef pair<int64,int64> pll;

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


int32 solve(string t, string s){
  if(s.size() == 0)return inf;
  if(t.size() == 0)return 0;
  int32 res = inf;
  REP(i,s.size()){
    if(s[i] == t[0]){
      int32 cnt = 0;
      string tmp = s;
      while(i > 0){
        swap(tmp[i],tmp[i-1]);
        ++cnt;
        --i;
      }
      chmin(res,cnt+solve(t.substr(1),tmp.substr(1)));
      break;
    }
  }
  REP(i,s.size()){
    if(s[i] > t[0]){
      chmin(res,i);
    }
  }
  return res;
}

int main(){
  cin.tie(0);
  ios::sync_with_stdio(false);
  int32 c;
  cin >> c;
  while(c--){
    string s;
    cin >> s;
    const string t = "atcoder";
    int32 ans = solve(t,s);
    if(ans == inf){
      ANS(-1);
    }else{
      ANS(ans);
    }
  }
  return 0;
}
#include <bits/stdc++.h>
#define pb push_back
#define mp make_pair
#define x first
#define y second

using namespace std;

typedef long long ll;

const string tmp = "atcoder";

int main()
{
 	//freopen("input.txt", "r", stdin);
 	//freopen("output.txt", "w", stdout);
 	ios_base::sync_with_stdio(0); cin.tie(0);
 	int test;
 	cin >> test;
 	for (int rep = 1; rep <= test; rep++)
 	{
 	 	string s;
 	 	cin >> s;
 	 	int n = (int) s.length();

 	 	int pos = -1;
 	 	for (int i = 0; i < n; i++) if (s[i] != 'a') {pos = i; break;}

 	 	if (pos == -1) {cout << "-1\n"; continue;}
 	 	if (tmp < s) {cout << "0\n"; continue;}
 	 	
 	 	if (s[pos] > 't') cout << pos - 1 << '\n';
 	 	else cout << pos << '\n';
 	}
 	return 0;
}