#include<iostream>
#include<algorithm>
#include<vector>
#include<queue>
using namespace std;
typedef long long ll;
#define rep(i,n) for(int i=0;i<n;i++)
#define chmin(a,b) a=min(a,b)
#define chmax(a,b) a=max(a,b)
#define mod 1000000007
#define ad(a,b) a=(a+b)%mod;
#define X 5010
ll po(ll x,ll y){
	ll res=1;
	for(;y;y>>=1){
		if(y&1)res=res*x%mod;
		x=x*x%mod;
	}
	return res;
}
ll fac[X],ivf[X];
void init(){
	fac[0]=1;
	for(ll i=1;i<X;i++)fac[i]=fac[i-1]*i%mod;
	for(ll i=0;i<X;i++)ivf[i]=po(fac[i],mod-2);
}
ll C(ll n,ll k){
	return fac[n]*ivf[n-k]%mod*ivf[k]%mod;
}

int solve(){
	string s;
	cin>>s;
	string ss=s;
	sort(ss.begin(),ss.end());
	reverse(ss.begin(),ss.end());
	string atc="atcoder";
	if(atc>=ss){
		return -1;
	}
	int ans=1e9;
	for(int i=0;i<=atc.size();i++){
		int cnt=0;
		ss=s;
		for(int j=0;j<i;j++){
			for(int k=j;k<ss.size();k++){
				if(ss[k]==atc[j]){
					cnt+=k-j;
					swap(ss[k],ss[j]);
					break;
				}
			}
		}
		if(i<atc.size()){
			for(int k=i;k<ss.size();k++){
				if(ss[k]>atc[i]){
					cnt+=k-i;
					swap(ss[k],ss[i]);
					break;
				}
			}
		}
		if(atc<ss){
			chmin(ans,cnt);
			//cerr<<i<<":"<<cnt<<endl;
		}
	}
	return ans;
}
int main(){
	cin.tie(0);
	ios::sync_with_stdio(0);
	ll t;
	cin>>t;
	while(t--)cout<<solve()<<"\n";
}

#include<bits/stdc++.h>
using namespace std;
#define endl '\n'
typedef long long int ll;

#define sim template < class c
#define ris return * this
#define dor > debug & operator <<
#define eni(x) sim > typename \
  enable_if<sizeof dud<c>(0) x 1, debug&>::type operator<<(c i) {
sim > struct rge { c b, e; };
sim > rge<c> range(c i, c j) { return rge<c>{i, j}; }
sim > auto dud(c* x) -> decltype(cerr << *x, 0);
sim > char dud(...);
struct debug {
#ifdef LOCAL
~debug() { cerr << endl; }
eni(!=) cerr << boolalpha << i; ris; }
eni(==) ris << range(begin(i), end(i)); }
sim, class b dor(pair < b, c > d) {
  ris << "(" << d.first << ", " << d.second << ")";
}
sim dor(rge<c> d) {
  *this << "[";
  for(auto it = d.b;it != d.e;++it)
    *this << ", " + 2 * (it == d.b) << *it;
  ris << "]";
}
#else
sim dor(const c&) { ris; }
#endif
};
#define imie(...) " [" << #__VA_ARGS__ ": " << (__VA_ARGS__) << "] "

int main(){
  ios::sync_with_stdio(false);
  cin.tie(nullptr);

  int t;cin >> t;
  while(t--){
    string s;cin >> s;
    string w = "atcoder";
    if(w < s)cout << 0;
    else{
      int ans = INT_MAX;
      for(int i = 0;i < (int)s.length() - 1;i++){
        for(int j = i + 1;j < (int)s.length();j++){
          swap(s[i], s[j]);
          if(w < s)ans = min(ans, abs(i - j));
          swap(s[i], s[j]);
        }
      }
      if(ans == INT_MAX)cout << -1;
      else cout << ans;
    }
    cout << endl;
  }
}
