#include <stdio.h>
#include <iostream>
#include <vector>
#include <queue>
#include <stack>
#include <algorithm>

using ll = long long int;
const int INF = (1<<30);
const ll INFLL = (1ll<<60);
const ll MOD = (ll)(1e9+7);

#define l_ength size

void mul_mod(ll& a, ll b){
	a *= b;
	a %= MOD;
}

void add_mod(ll& a, ll b){
	a = (a<MOD)?a:(a-MOD);
	b = (b<MOD)?b:(b-MOD);
	a += b;
	a = (a<MOD)?a:(a-MOD);
}

int main(void){
	int q,n,k;
	bool flag;
	std::string s;
	std::cin >> q;
	while(q--){
		std::cin >> s;
		flag = false;
		n = s.l_ength();
		if(s > "atcoder"){
			std::cout << 0 << std::endl;
			continue;
		}
		for(k=1; k<n; ++k){
			if(s[k]>'a'){
				flag = true;
				break;
			}
		}
		if(!flag){
			std::cout << (-1) << std::endl;
			continue;
		}
		if(s[k]>'t'){
			std::cout << k-1 << std::endl;
		}else{
			std::cout << k << std::endl;
		}
	}
	return 0;
}


#pragma GCC optimize("Ofast","inline","-ffast-math")
#pragma GCC target("avx,sse2,sse3,sse4,mmx")
#include<bits/stdc++.h>
#define int long long
#define pb push_back
#define pf push_front
#define F first
#define S second
#define SS stringstream
#define sqr(x) ((x)*(x))
#define m0(x) memset(x,0,sizeof(x))
#define m1(x) memset(x,63,sizeof(x))
#define CC(x) cout << (x) << endl
#define AL(x) x.begin(),x.end()
#define pw(x) (1ull<<(x))
#define NMSL cout << "NMSL" << endl;
#define debug(x) cout << #x << ": " << x << endl;
#define debug2(x, y) cout <<#x<<": "<<x<<" | "<<#y<<": "<<y<<endl;
#define debug3(x, y, z) cout <<#x<<": "<<x<<" | "<<#y<<": "<<y<<" | "<<#z<<": "<<z<<endl;
#define debug4(a, b, c, d) cout <<#a<<": "<<a<<" | "<<#b<<": "<<b<<" | "<<#c<<": "<<c<<" | "<<#d<<": "<<d<<endl;
#define fio ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define in128 __int128_t
using namespace std;
const int N = 1e5+10;
const int INF = 0x7f7f7f7f;
const double EPS = 1e-3;
const long double PI = 3.14159265358979323846;
int a[N];
signed main()
{
    fio
    int T;
    string ss;
    string at="atcoder";
    cin>>T;
    while(T--)
    {
        cin>>ss;
        string sk=ss;
        sort(sk.begin(),sk.end());
        reverse(sk.begin(),sk.end());
        if(sk<=at)
        {
            cout<<-1<<endl;
            continue;
        }
        int ans=INF;
        if(ss>at)ans=0;
        else if(ss==at)ans=1;
        else
        {
            for(int i=0;i<ss.length();++i)
            {
                for(int j=i+1;j<ss.length();++j)if(ss[j]>at[i]){ans=min(ans,j-i);break;}
                if(ss[i]<at[i])break;
            }
        }
        cout<<ans<<endl;
    }
    return 0;
}
