# include <bits/stdc++.h>
using namespace std;
# define rep(i,a,n) for (int i=a; i<=n; ++i)
# define per(i,a,n) for (int i=a; i>=n; --i)
# define bug puts("H");
# define pb push_back
# define mp make_pair
# define all(x) (x).begin(), (x).end()
# define SZ(x) (int)x.size()
# define fi first
# define se second
# define lch p<<1,l,mid
# define rch p<<1|1,mid+1,r
# define mem(a,b) memset(a,b,sizeof(a))
# define INF 1000000001
# define MOD 1000000007
typedef vector<int> Vi;
typedef long long i64;
typedef pair<int, int> Pi;
mt19937 mrand(random_device{}());
int rnd(int x) {return mrand()%x;}
i64 powmod(i64 a, i64 b){i64 res=1;a%=MOD;assert(b>=0);for(;b;b>>=1){if(b&1)res=res*a%MOD;a=a*a%MOD;}return res;}
i64 Inv(i64 b){return powmod(b,(i64)MOD-2);}
typedef long long ll;

const int N = 100005;
const double eps = 1e-9;
const double inf = 1e18;

char s[1005];

int main ()
{
    int T;
    cin >> T;
    string str = "atcoder", tmp;
    while (T--) {
        cin >> tmp;
        int ans = INF;
        if (str < tmp) ans = 0;
        else {
            int sz = SZ(tmp);
            rep(i,1,sz-1) {
                rep(j,0,min(i-1,6)) {
                    string t;
                    if (j) t += tmp.substr(0,j);
                    t += tmp[i];
                    t += tmp.substr(j,i-j);
                    t += tmp.substr(i+1,sz-1-i);
                    if (str < t) ans = min(ans, i-j);
                    //cout << i << " " << j << " " << t << endl;
                }
            }
        }
        if (ans == INF) cout << -1 << endl;
        else cout << ans << endl;
    }
    return 0;
}

#include "iostream"
#include "climits"
#include "list"
#include "queue"
#include "stack"
#include "set"
#include "functional"
#include "algorithm"
#include "string"
#include "map"
#include "unordered_map"
#include "unordered_set"
#include "iomanip"
#include "cmath"
#include "random"
#include "bitset"
#include "cstdio"
#include "numeric"
#include "cassert"
#include "ctime"

using namespace std;

constexpr long long int MOD = 1000000007;
//constexpr int MOD = 1000000007;
//constexpr int MOD = 998244353;
//constexpr long long int MOD = 998244353;
constexpr double EPS = 1e-12;

int N, M, K, T, H, W, L, R;
//long long int N, M, K, T, H, W, L, R;


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

	cin >> T;
	string t = "atcoder";
	while (T--) {
		string s;
		cin >> s;
		if (t < s) {
			cout << 0 << endl;
			continue;
		}
		int ans = MOD;
		for (int i = 0; i < s.size() && i < t.size(); i++) {
			for (int j = i + 1; j < s.size(); j++) {
				if (t[i] < s[j]) {
					ans = min(ans, j - i);
				}
			}
			if (s[i] != t[i])break;
		}
		if (ans == MOD)ans = -1;
		cout << ans << endl;
	}
}