#include <bits/stdc++.h>
using namespace std;
int PREP = (cin.tie(nullptr), ios::sync_with_stdio(false), cout << fixed << setprecision(12), 0);
int solve(string S) {
    if ("atcoder" < S) {
        return 0;
    }
    for (int i = 0; i < S.size(); i++) {
        if (S[i] > 't') {
            return i - 1;
        } else if (S[i] > 'a') {
            return i;
        }
    }
    return -1;
}
int main() {
    int T; cin >> T;
    while (T--) {
        string S; cin >> S;
        cout << solve(S) << '\n';
    }
    return 0;
}

#pragma GCC optimize(2)
#include<iostream>
#include<unordered_map>
#include<algorithm>
#include<cstring>
#include<cstdio>
#include<string>
#include<vector>
#include<queue>
#include<stack>
#include<cmath>
#include<map>
#include<set>
#define Buff ios::sync_with_stdio(false), cin.tie(0), cout.tie(0)
#define rush() int Case = 0; int T; cin >> T;  while(T--)
#define rep(i, a, b) for(int i = a; i <= b; i ++)
#define per(i, a, b) for(int i = a; i >= b; i --)
#define reps(i, a, b) for(int i = a; b; i ++)
#define clc(a, b) memset(a, b, sizeof(a))
#define Buff ios::sync_with_stdio(false)
#define readl(a) scanf("%lld", &a)
#define readd(a) scanf("%lf", &a)
#define readc(a) scanf("%c", &a)
#define reads(a) scanf("%s", a)
#define read(a) scanf("%d", &a)
#define lowbit(n) (n&(-n))
#define pb push_back
#define sqr(x) x*x
#define lson rt<<1
#define rson rt<<1|1
#define ls lson, l, mid
#define rs rson, mid+1, r
#define y second
#define x first
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int>PII;
const int mod = 1e9+7;
const double eps = 1e-6;
const int N = 1e6+7;
string s;
string t = "atcoder";
bool judge()
{
    reps(i, 0, s[i])    if(s[i] != 'a') return false;
    return true;
}
int main()
{
    int T;
    cin >> T;
    while(T --)
    {
        cin >> s;
        if(judge())
        {
            puts("-1");
            continue;
        }
        int res = mod;
        for(int i = 0; s[i]; i ++)
        {
            for(int j = i; s[j]; j ++)
            {
                swap(s[i], s[j]);
                if(s > t)
                {
                    res = min(res, j-i);
                    swap(s[i], s[j]);
                    break;
                }
                swap(s[i], s[j]);
            }
        }
        cout << res <<endl;
    }
	return 0;
}
/*
aaaaaaatcoder
*/
