#include<bits/stdc++.h>
#include<cstdlib>
#include <algorithm>
#include <string>
using ll = long long;
using ull = unsigned long long;
using namespace std;
constexpr int inf = 1<<30;
constexpr int mo = 1e9+7;
constexpr ll infl = 1ll<<60;

int main(){
    cin.tie(nullptr);
    std::ios::sync_with_stdio(false);
    
    int n;
    cin>>n;
    string t = "atcoder";
    for(int i=0;i<n;++i){
        string s;
        cin>>s;
        int m = (int)s.size();
        if(s > t){
            cout << 0 << endl;
            continue;
        }
        int num = inf;
        for(int i=0;i<m;++i){
            if(s[i] != 'a'){
                swap(s[1],s[i]);
                if(s > t) num = min(num,i-1);
                swap(s[1],s[i]);
                swap(s[0],s[i]);
                if(s > t) num = min(num,i);
                swap(s[0],s[i]);
                break;
            }
        }
        if(num == inf)cout << -1 << endl;
        else cout << num << endl;
        
    }
    return 0;
    
}
#pragma GCC optimize("Ofast")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,sse4.1,sse4.2,popcnt,mmx,avx")
#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
typedef long double ld;
#define PB push_back
#define MP make_pair
#define endl "\n"
#define fst first
#define snd second
const int UNDEF = -1;
const int INF=1<<30;
template<typename T> inline bool chkmax(T &aa, T bb) { return aa < bb ? aa = bb, true : false; }
template<typename T> inline bool chkmin(T &aa, T bb) { return aa > bb ? aa = bb, true : false; }
typedef pair<ll,ll> pll;typedef vector<ll> vll;typedef pair<int,int> pii;typedef vector<int> vi;typedef vector<vi> vvi;typedef vector<pii> vpii;typedef vector<pll> vpll;
template<typename T> void makeunique(vector<T> &vx) {sort(vx.begin(),vx.end());auto it=unique(vx.begin(),vx.end());vx.resize(std::distance(vx.begin(),it));}

string t="atcoder";
int go() {
	string s; cin>>s;
	if (t<s) return 0;
	int ans=INF;
	for (int si=1;si<s.length();si++) if (s[si]>'a') chkmin(ans,si);
	if (s[0]=='a') {
		for (int si=2;si<s.length();si++) if (s[si]>'t') chkmin(ans,si-1);
	}
	if (ans>=INF/2)ans=-1;
	return ans;
}


// Generated by 1.1.7.1 https://github.com/kyuridenamida/atcoder-tools  (tips: You use the default template now. You can remove this line by using your custom template)
int main(){
    int t; cin>>t;
    for (int i=0;i<t;i++) {
    	int ans=go();
    	cout<<ans<<endl;
    }
}
