#include <iostream>
#include <string>
#include <algorithm>
using namespace std;

string s;
bool cmp(char a, char b){
	return a > b;
}
int main () {
	int T; cin >> T;
	string cp = "atcoder";
	while (T --){
		cin >> s;
		string temp = s;
		sort(temp.begin(), temp.end(), cmp);
		if(temp < cp){
			cout << -1 <<"\n";
			continue;
		}
		int n = s.length();
		if(s > cp){
			cout << 0 << "\n";
			continue;
		}
		int ans = 0;
		int p = 0;
		for(int i = 0; i < n; ++i){
			if(s[i] > 'a'){
				p = i;
				break;
			}
		}
		if(s[p] > 't') p --;
		cout << p << "\n";
	}
	return 0;
}
#include<bits/stdc++.h>
using namespace std;
// #include <ext/pb_ds/assoc_container.hpp> 
// #include <ext/pb_ds/detail/standard_policies.hpp>
// using namespace __gnu_pbds;

#pragma GCC optimize("O3")
#ifdef LOCAL
#include "/Users/lbjlc/Desktop/coding/debug_utils.h"
#else
#define print(...) ;
#define printn(...) ;
#define printg(...) ;
#define fprint(...) ;
#define fprintn(...) ;
#endif

#define rep(i, a, b) for(auto i = (a); i < (b); i++)
#define rrep(i, a, b) for(auto i = (a); i > (b); i--)
#define all(v) (v).begin(), (v).end()
#define pb push_back
// #define mp make_pair
#define fi first
#define se second
#define maxi(x, y) x = max(x, y)
#define mini(x, y) x = min(x, y)
// long long fact(long long n) { if(!n) return 1; return n*fact(n-1); }
// #define endl '\n'
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
int get_random() {
    static uniform_int_distribution<int> dist(0, 1e9 + 6);
    return dist(rng);
}
#define solve_testcase int T;cin>>T;for(int t=1;t<=T;t++){solve(t);}


typedef unsigned long long ull;
typedef long long ll;
 
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef pair<double, double> pdd;
 
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<vvi> vvvi;
typedef vector<pii> vpii;
typedef vector<pll> vpll;
typedef vector<pdd> vpdd;
typedef vector<long long> vll;
#define bd(type,op,val) bind(op<type>(), std::placeholders::_1, val)

template<class T>
void make_unique(T & v) {
    sort(v.begin(), v.end()); v.erase(unique(v.begin(), v.end()), v.end());
}
int geti() { int x; cin >> x; return x; }
long long getll() { long long x; cin >> x; return x; }
double getd() { double x; cin >> x; return x; }
// pair<int, int> a(geti(), geti()) does not work
// pair<int, int> a({geti(), geti()}) works, since it uses initializer.

const int MAXN = 3e5 + 100;

ll cal(const string & s1, string s2) {
	if(s2.empty()) return 1e9;
	if(s1.empty()) return 0;
	if(s1[0]<s2[0]) return 0;

	
	ll res=1e9;
	if(s1[0]==s2[0]) 
		res = cal(s1.substr(1, s1.size()-1), s2.substr(1, s2.size()-1));
	rep(i,0,s2.size()){
		if(s2[i]>s1[0]) {
			res = min(res,ll(i));
			break;
		}
	}
	if(s1[0]==s2[0]) return res;
	// print(s1,s2);
	rep(i,1,s2.size()) {
		if(i>res) break;
		if(s2[i]==s1[0]) {
			auto tmp = s2[i] + s2.substr(0, i) + s2.substr(i+1, int(s2.size()) - (i+1));
			res = min(res, i + cal(s1.substr(1,s1.size()-1), tmp.substr(1,tmp.size()-1)));
			break;
		}
	}
	return res;
}

void solve(int tt) {
    // cout<<"Case #"<<tt<<": ";
    string s;
    cin>>s;
    ll res = cal("atcoder", s);
    cout<<(res >= 1e9 ? -1 : res)<<endl;
}

int main(int argc, char * argv[]) {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    cout.tie(nullptr);
    solve_testcase;



    return 0;
}











