#pragma GCC optimize("Ofast")
#if 1
#define _USE_MATH_DEFINES
#include "bits/stdc++.h"

using namespace std;

using ll = int64_t;
using db = double;
using ld = long double;
using vi = vector<int>;
using vl = vector<ll>;
using vvi = vector<vector<int>>;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
using vpii = vector<pii>;
using vpll = vector<pll>;

constexpr char newl = '\n';
constexpr double eps = 1e-10;

#define FOR(i,a,b) for (int i = (a); i < (b); i++)
#define F0R(i,b) FOR(i,0,b)
#define RFO(i,a,b) for (int i = ((b)-1); i >=(a); i--)
#define RF0(i,b) RFO(i,0,b)
#define show(x) cerr << #x << " = " << (x) << '\n';
#define rng(a) a.begin(),a.end()
#define rrng(a) a.rbegin(),a.rend()
#define sz(x) (int)(x).size()
#define YesNo(x) cout<<((x)?"Yes":"No")<<newl;
#define YESNO(x) cout<<((x)?"YES":"NO")<<newl;
#define v(T) vector<T>
#define vv(T) vector<vector<T>>

template<class A, class B> using umap = unordered_map<A, B>;
template<class A> using uset = unordered_set<A>;

template<typename T1, typename T2> inline void chmin(T1& a, T2 b) { if (a > b) a = b; }
template<typename T1, typename T2> inline void chmax(T1& a, T2 b) { if (a < b) a = b; }
template<class T> bool lcmp(const pair<T, T>& l, const pair<T, T>& r) {
  return l.first < r.first;
}
template<class T> istream& operator>>(istream& i, v(T)& v) {
  F0R(j, sz(v)) i >> v[j];
  return i;
}
template<class A, class B> istream& operator>>(istream& i, pair<A, B>& p) {
  return i >> p.first >> p.second;
}
template<class A, class B, class C> istream& operator>>(istream& i, tuple<A, B, C>& t) {
  return i >> get<0>(t) >> get<1>(t) >> get<2>(t);
}
template<class T> ostream& operator<<(ostream& o, const pair<T, T>& v) {
  o << "(" << v.first << "," << v.second << ")";
  return o;
}
template<class T> ostream& operator<<(ostream& o, const vector<T>& v) {
  F0R(i, v.size()) {
    o << v[i] << ' ';
  }
  o << newl;
  return o;
}
template<class T> ostream& operator<<(ostream& o, const set<T>& v) {
  for (auto e : v) {
    o << e << ' ';
  }
  o << newl;
  return o;
}
template<class T1, class T2> ostream& operator<<(ostream& o, const map<T1, T2>& m) {
  for (auto& p : m) {
    o << p.first << ": " << p.second << newl;
  }
  o << newl;
  return o;
}

const string target = "atcoder";
int inf = 1e9;

// 文字列のt文字目を、t文字以降とのswapのみであるchar以上にする。
// その後swap回数を返す
int F(string& S, int t, char c) {
  int u = -1;
  FOR(i, t, sz(S)) {
    if (S[i] >= c) {
      u = i;
      break;
    }
  }
  if (u < 0) return inf;
  int ans = u - t;
  while (u > t) {
    swap(S[u], S[u - 1]);
    u--;
  }
  return ans;
}

ll solve() {
  string S; cin >> S;
  ll ans = inf;
  ll base = 0; // 既にSに加えた処理回数
  F0R(i, min(sz(target), sz(S))) {
    string s(S);
    chmin(ans, base + F(s, i, target[i] + 1));
    base += F(S, i, target[i]);
  }
  if (sz(S) > sz(target)) {
    chmin(ans, base);
  }
  return ans;
}

// INSERT ABOVE HERE
signed main() {
  cin.tie(0);
  ios_base::sync_with_stdio(false);

  int T; cin >> T;
  F0R(t, T) {
    ll ans = solve();
    if (ans >= inf) ans = -1;
    cout << ans << newl;
  }
}
#endif

#include <bits/stdc++.h>
using namespace std;
string mu = "atcoder", s;
int wei = 0, anss = 0;
int Find(char a) {
	for(int i = 0; i < s.size(); ++i) {
		if(s[i] > a) return wei;
	}
	return 0x3f3f3f3f;
}



int main() {
	int t; cin >> t;
	while(t--) {
		anss = 0x3f3f3f3f;
		cin >> s;
		if(s > mu) anss = 0;
		if(s.size() == 1) {
			if(anss == 0x3f3f3f3f) cout << -1 << endl;
			else cout << anss << endl;
			continue;
		}
		if(s[1] > 'a') anss = min(anss, 1);
//		if(s[1] == 'a') {
		if(s[0] == 'a') for(int i = 1; i < s.size(); ++i) {
			if(s[i] > 't') anss = min(i - 1, anss);
		}
		for(int i = 0; i < s.size(); ++i) {
			if(s[i] > 'a') anss = min(i, anss);
		}
//		}
		if(anss == 0x3f3f3f3f) cout << -1 << endl;
		else cout << anss << endl;
	}
//	cout << "atcodeer" > "atcoder" << endl;
} 