#include<bits/stdc++.h>
using namespace std;

typedef long long LL;

const int N=1000;
const char str[10]=" atcoder";

char s[N+9];
int n;

void into(){
  scanf("%s",s+1);
  n=strlen(s+1);
}

int ans;

void Get_ans(){
  ans=0;
  int flag=0;
  for (int i=1;i<=n&&i<=7;++i)
	if (s[i]>str[i]) return;
    else if (s[i]<str[i]) {flag=1;break;}
  if (!flag&&n>7) return;
  for (int i=1;i<=n;++i)
	if (s[i]^'a'){
	  ans=s[i]>'t'?i-2:i-1;
	  return;
	}
  ans=-1;
}

void work(){
  Get_ans();
}

void outo(){
  printf("%d\n",ans);
}

int main(){
  int T;
  scanf("%d",&T);
  for (;T--;){
    into();
    work();
    outo();
  }
  return 0;
}
#include <iostream>
#include <vector>
#include <algorithm>
#include <cmath>
#include <string>
#include <queue>
#include <stack>
#include <set>
#include <map>
#include <iomanip>
#include <utility>
#include <tuple>
#include <functional>
#include <bitset>
#include <cassert>
#include <complex>
#include <stdio.h>
#include <time.h>
#include <numeric>
#include <random>
#include <unordered_map>
#include <unordered_set>
#define all(a) a.begin(),a.end()
#define rep(i, n) for (ll i = 0; i < (n); i++)
#define pb push_back
#define debug(x) cerr << __LINE__ << ' ' << #x << ':' << (x) << '\n'
#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")
using namespace std;
typedef long long ll;
typedef unsigned int uint;
typedef unsigned long long ull;
typedef long double ld;
typedef pair<ll, ll> P;
typedef complex<ld> com;
template<class T> using prique = priority_queue<T, vector<T>, greater<T>>;
constexpr int inf = 1000000010;
constexpr ll INF = 1000000000000000010;
constexpr int mod1e9 = 1000000007;
constexpr int mod998 = 998244353;
constexpr ld eps = 1e-12;
constexpr ld pi = 3.141592653589793238;
constexpr ll ten(int n) { return n ? 10 * ten(n - 1) : 1; };
int dx[] = { 1,0,-1,0,1,1,-1,-1 }; int dy[] = { 0,1,0,-1,1,-1,1,-1 };
void fail() { cout << "-1\n"; exit(0); } void no() { cout << "No\n"; exit(0); }
template<class T, class U> inline bool chmax(T &a, const U &b) { if (a < b) { a = b; return true; } return false; }
template<class T, class U> inline bool chmin(T &a, const U &b) { if (a > b) { a = b; return true; } return false; }
template<class T> istream &operator >> (istream &s, vector<T> &v) { for (auto &e : v) s >> e; return s; }
template<class T> ostream &operator << (ostream &s, const vector<T> &v) { for (auto &e : v) s << e << ' '; return s; }

struct fastio {
	fastio() {
		cin.tie(0); cout.tie(0);
		ios::sync_with_stdio(false);
		cout << fixed << setprecision(20);
		cerr << fixed << setprecision(20);
	}
}fastio_;

int main() {
	int t;
	cin >> t;
	while (t--) {
		string s;
		cin >> s;
		int n = s.size();
		string atcoder = "atcoder";
		if (s > atcoder) {
			cout << "0\n";
			continue;
		}
		int ans = inf;
		int tmp = 0;
		for (int i = 0; i < min(7, n); i++) {
			int r = -1;
			for (int j = i; j < n; j++) {
				if (s[j] > atcoder[i]) {
					r = j;
					break;
				}
			}
			if (r != -1) {
				chmin(ans, tmp + r - i);
			}
			int q = -1;
			for (int j = i; j < n; j++) {
				if (s[j] == atcoder[i]) {
					q = j;
					break;
				}
			}
			for (int j = q; j > i; j--) swap(s[j], s[j - 1]);
			if (q != -1) tmp += q - i;
			else tmp = inf;
		}
		cout << (ans == inf ? -1 : ans) << '\n';
	}
}