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

//#pragma GCC optimize("O3")
 
#define DIM 200009
#define INF ((long long)1e18+9ll)
#define pairll pair<long long,long long>
#define fi first
#define se second
#define ld long double
#define amen exit(0)
#define endl '\n'
#define MODULO 998244353
#define fast ios_base::sync_with_stdio(false),cin.tie(0),cout.tie(0)
 
long long i,j,k,l,n,m;

string a,b;

int main()
{
    fast;

    int nt;
    cin>>nt;
    while(nt--){
        a="atcoder";
        cin>>b;
        if(a<b)cout<<0<<endl;
        else{
            long long res=INF;
            long long pos=-1;
            for(int i=0;i<b.size();i++){
                if(b[i]!='a'){
                    pos=i+1;
                    break;
                }
            }

            if(pos==-1)cout<<-1<<endl;
            else{
                if(b[pos-1]>'t')cout<<pos-2<<endl;
                else cout<<pos-1<<endl;
            }
        }
    }

    amen;
}


#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
#include<sstream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<climits>
#include<cmath>
#include<string>
#include<vector>
#include<set>
#include<map>
#include<queue>
#include<numeric>
#include<functional>
#include<algorithm>
#include<bitset>
#include<tuple>
#include<unordered_set>
#include<unordered_map>
#include<random>
#include<array>
#include<cassert>
using namespace std;
#define INF ((1<<30)-1)
#define rep(i,n) for(int i=0;i<(int)(n);i++)
#define all(v) v.begin(),v.end()




int main() {
	ios::sync_with_stdio(0);
	cin.tie(0);

	int T;
	cin >> T;
	rep(_, T) {
		string s;
		cin >> s;

		string t = s;
		sort(all(t));
		reverse(all(t));
		if (t <= "atcoder") {
			cout << -1 << endl;
			continue;
		}

		int ans = INF;
		int cur = 0;
		rep(i, 7) {
			int idx = -1;
			for (int j = i; j <= s.size(); j++) {
				if ("atcoder"[i] < s[j]) {
					ans = min(ans, cur + j - i);
				}
				else if ("atcoder"[i] == s[j] && idx == -1) {
					idx = j;
				}
			}
			if (idx == -1)break;
			cur += idx - i;
			for (int j = idx - 1; j >= i; j--) {
				s[j + 1] = s[j];
			}
			s[i] = "atcoder"[i];
		}
		if (s.size() > 7 && s.substr(0, 7) == "atcoder")ans = min(ans, cur);
		cout << ans << endl;
	}


	return 0;
}
