#include <bits/stdc++.h>
// clang-format off
using namespace std; using ll = long long; const ll INF = 9e18;
void print() { cout << endl; }
template<typename Head,typename... Tail> void print(Head head,Tail... tail){cout<<head;if(sizeof...(Tail)>0)cout<<" ";print(tail...);}
void print0() {}
template<typename Head,typename... Tail> void print0(Head head,Tail... tail){cout<<head;print0(tail...);}
// clang-format on
 
int main() {
  int n;
  string s;
  cin >> n;
  vector<char> atcoder={'a','t','c','o','d','e','r'};
  for (int i = 0; i < n; i++) {
    cin >> s;
    int lg = -1;
    int gta = 4000;
    int gtt = 4000;
    for (int j = 0; j < s.length(); j++) {
      if (j<=6 && lg==-1 && s.at(j)>atcoder.at(j) ) lg = 1;
      if (j<=6 && lg==-1 && s.at(j)<atcoder.at(j) ) lg = 0;
	  if (j>=7 && lg==-1) lg = 1;
      if (s.at(j)>'a') gta=min(gta,j);
      if (s.at(j)>'t') gtt=min(gtt,j);
    }
    if(lg==1){
      print(0);
    }else if (gtt<4000){
      print(min(gtt-1,gta));
    }else if (gta<4000){
      print(gta);
    }else{
      print(-1);
    }
  }
}
#include <bits/stdc++.h>
#include <bits/extc++.h>
#define ll long long
#define endl "\n"
#define pb push_back
#define ms(v,x) memset(v,x,sizeof(v))
#define ff first
#define ss second
#define td(v) v.begin(),v.end()
#define rep(i,a,n) for (int i=(a);i<(n);i++)
#define re(v) {for(auto &_re : v) cin >> _re;}
#define pr(v) {for(auto _pr : v) cout << _pr << " "; cout << endl;}
#define sz(x) (int)(x).size()
#define pii pair<int,int>
#define pll pair<ll,ll>
#define vi vector<int>
#define vl vector<ll>
#define eb emplace_back
using namespace std;
const ll M = 1e9 + 7;
const ll mod = 998244353;
mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());
ll binpow(ll a, ll b){
	ll res = 1;
	while(b > 0){
		if(b&1) res = res * a % M;
		a = a * a % M;
		b>>=1;
	}
	return res;
}
//#define double long double
//#define int long long
string t = "atcoder";
void solve(){
	string s;
	cin >> s;
	bool ok = false;
	for(char c : s) if(c!='a') ok = true;
	if(!ok){
		cout << -1 << endl;
		return;
	}
	if(t < s){
		cout << 0 << endl;
		return;
	}
	int eq = 1;
	for(int i=0;i<min((int)s.size(), 7);i++){
		if(s[i] == t[i]){
			eq++;
		}
	}
	int ans = 1e9;
	for(int i=0;i<s.size();i++){
		for(int j=0;j<eq; j++){
			if(s[i] > t[j]){
				ans = min(ans, abs(i-j));
			}
		}
	}
	cout << ans << endl;
}	
int32_t main(){
  ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
  //freopen("jump.in", "r", stdin);
	//freopen("equal.out", "w", stdout);
	int t = 1;
	cin >> t;
	while(t--){
		solve();
	}
}