#include<algorithm>
#include<iostream>
#include<cstdio>
#define int long long
int t;
std::string st,goal="atcoder";
bool can(int len){
	for(int i=0;i<st.length()-len;i++){
		std::swap(st[i],st[len+i]);
		if(st>goal)return true;
		std::swap(st[i],st[len+i]);
	}
	return false;
}
signed main(){
	scanf("%lld",&t);
	for(int wc=1;wc<=t;wc++){
		std::cin>>st;
		if(st>goal){
			puts("0");
			continue;
		}
		bool flag=false;
		for(int i=1;i<=st.length()-1;i++)if(can(i)){
			printf("%lld\n",i);
			flag=true;
			break;
		}
		if(!flag)puts("-1");
	}
	return 0;
}
#include <bits/stdc++.h>
using namespace std;

#define ll long long
#define mp make_pair
#define pb push_back

#define eprintf(...) fprintf(stderr, __VA_ARGS__)
#define rep(i, n) for (int i = 0; i < (int)(n); ++ i)

int main() {
	auto solve = [&] () {
		static char s[1005];
		scanf("%s", s);
		int n = strlen(s);
		const char *t = "atcoder";
		int m = strlen(t);
		int ans = 0x3f3f3f3f, cur = 0;
		bool full = 1;
		rep(i, min(n, m)) {
			int mneq = n, mngeq = n;
			for (int j = i; j < n; ++ j) {
				if (s[j] == t[i]) mneq = min(mneq, j);
				if (s[j] > t[i]) mngeq = min(mngeq, j);
			}
			if (mngeq < n) ans = min(ans, cur + mngeq - i);
			if (mneq < n) cur += mneq - i;
			else { full = 0; break; }
		}
		if (full && n > m) ans = min(ans, cur);
		if (ans >= 1e9) printf("-1\n");
		else printf("%d\n", ans);
	};
	int T; scanf("%d", &T);
	while (T --) solve();
	return 0;
}
