#include <iostream>
#include <string>
#include <vector>
#include <queue>
#include <deque>
#include <stack>
#include <set>
#include <map>
#include <unordered_map>
#include <unordered_set>
#include <cstring>
#include <cmath>
#include <cstdlib>
#include <algorithm>
#include <random>
#include <iomanip>
#include <functional>
#include <cassert>
#include <bitset>
#include <chrono>
 
using namespace std;
 
typedef long long ll;

const int Inf = 1e9;

int main() {
  ios_base::sync_with_stdio(false); cin.tie(0);
#ifdef LOCAL
  freopen("input.txt", "r", stdin);
#endif
  int t;
  cin >> t;
  string go = "atcoder";
  while (t--) {
    string s;
    cin >> s;
    int need = 0;
    int ans = Inf;
    for (int i = 0; i < (int)go.size(); ++i) {
      for (int j = i; j < (int)s.size(); ++j) {
        if (s[j] > go[i]) {
          ans = min(ans, need + j - i);
        }
      }
      int id = -1;
      for (int j = i; j < (int)s.size(); ++j) {
        if (s[j] == go[i]) {
          id = j;
          break;
        }
      }
      if (id == -1) break;
      s.erase(s.begin() + id);
      s.insert(s.begin() + i, go[i]);
      need += id - i;

      if (i == (int)go.size() - 1) {
        if (s.size() > go.size()) {
          ans = min(ans, need);
        }
      }
    }
    if (ans == Inf) {
      cout << "-1\n";
    } else {
      cout << ans << '\n';
    }
  } 
}


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

void solve()
{
	string s;
	cin>>s;
	int n=s.length(),x=0;
	while(x<n && s[x]=='a')
		x++;
	cout<<(x==n?-1:s>"atcoder"?0:x-(s[x]>'t'))<<endl;
}

int main() {
	//freopen("t","r",stdin);
	int test;
	cin>>test;
	while(test--)
		solve();
    return 0;
}