using namespace std;
#include <cstdio>
#include <cstring>
#include <algorithm>
#define INF 1000000000
#define N 1010
int n,m=7;
char t[N]="atcoder";
char s[N];
int main(){
//	freopen("in.txt","r",stdin);
	int Q;
	scanf("%d",&Q);
	while (Q--){
		scanf("%s",s);
		n=strlen(s);
		int c=0,ans=INF;
		bool BZ=1;
		for (int i=0;i<m && BZ;++i){
			for (int j=i;j<n;++j)
				if (s[j]>t[i]){
					ans=min(ans,c+j-i);
					break;
				}
			bool bz=0;
			
			for (int j=i;j<n;++j)
				if (s[j]==t[i]){
					c+=j-i;
					for (;j>i;--j)
						swap(s[j],s[j-1]);
					bz=1;
					break;
				}
			
//			bz=(s[i]==t[i]);
			if (bz==0){
				BZ=0;
				break;
			}
		}
		if (BZ && n>m)
			ans=min(ans,c);
		printf("%d\n",ans==INF?-1:ans);
	}
	return 0;
}
#ifdef __GNUC__
#pragma GCC diagnostic ignored "-Wunused-result"
#pragma GCC target("avx2")
#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")
#endif

#include <algorithm>
#include <cmath>
#include <cstring>
#include <cctype>
#include <iostream>
#include <sstream>
#include <numeric>
#include <map>
#include <set>
#include <queue>
#include <vector>

using namespace std;

const int INF = 1 << 30;

const string ac = "atcoder";

int solve(string s) {
	int len = s.length();
	if (s > ac) {
		return 0;
	}
	for (int i = 1; i < len; ++i) {
		if (s[i] != 'a') {
			return i - (s[i] > 't');
		}
	}
	return -1;
}

int main() {
	int T;
	cin >> T;
	for (int t = 0; t < T; ++t) {
		string s;
		cin >> s;
		int ans = solve(s);
		cout << ans << endl;
	}
	return 0;
}
