#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
#define fi first
#define se second
#define rep(i, n) for (ll i = 0; i < (n); ++i)
#define rep0(i, n) for (ll i = 0; i <= (n); ++i)
#define rep1(i, n) for (ll i = 1; i <= (n); ++i)
#define rrep1(i, n) for (ll i = (n); i > 0; --i)
#define rrep0(i, n) for (ll i = (n); i >= 0; --i)
#define srep(i, s, t) for (ll i = s; i < t; ++i)
#define srep1(i, s, t) for (ll i = s; i <= t; ++i)
#define rsrep1(i, s, t) for (ll i = s; i >= t; --i)
#define rng(a) a.begin(), a.end()
#define rrng(a) a.rbegin(), a.rend()
#define isin(x, l, r) ((l) <= (x) && (x) < (r))
#define pb push_back
#define eb emplace_back
#define sz(x) (int)(x).size()
#define pcnt __builtin_popcountll
#define uni(x) x.erase(unique(rng(x)), x.end())
#define snuke srand((unsigned)clock() + (unsigned)time(NULL));
#define show(x) cerr << #x << " = " << x << endl;
#define PQ(T) priority_queue<T, v(T), greater<T>>
#define bn(x) ((1 << x) - 1)
#define dup(x, y) (((x) + (y)-1) / (y))
#define newline puts("")
#define v(T) vector<T>
#define vv(T) v(v(T))
#define min(a, b) (((a) < (b)) ? (a) : (b))
#define max(a, b) (((a) > (b)) ? (a) : (b))
typedef unsigned uint;
typedef unsigned long long ull;
typedef pair<ll, ll> P;
typedef tuple<ll, ll, ll> T;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<ll> vl;
typedef vector<P> vp;
typedef vector<T> vt;
typedef vector<string> vs;
template <typename T>
istream &operator>>(istream &i, v(T) & v)
{
    rep(j, sz(v)) i >> v[j];
    return i;
}
#define dame        \
    {               \
        puts("-1"); \
        return 0;   \
    }
#define yn           \
    {                \
        puts("Yes"); \
    }                \
    else { puts("No"); }
constexpr ll MAX = 2000001;
constexpr ll LINF = 9223372036854775807;
constexpr ll mod = 1000000007;
//constexpr ll mod = 998244353;

int main()
{
    ll n;
    cin >> n;
    rep(i, n)
    {
        string s;
        cin >> s;
        if ("atcoder" < s)
        {
            cout << 0 << endl;
            continue;
        }
        string t = s;
        sort(t.begin(), t.end());
        reverse(t.begin(), t.end());
        if ("atcoder" > t)
        {
            cout << -1 << endl;
            continue;
        }
        rep(i, s.size())
        {
            if (s.at(i) != 'a')
            {
                if (s.at(i) > 't')
                    cout << i - 1 << endl;
                else
                    cout << i << endl;
                break;
            }
        }
    }
    return 0;
}
#include<bits/stdc++.h>

using namespace std;

void solve()
{
    string s;
    cin>>s;
    int n=s.size();
    s="."+s;
    string t=".atcoder";
    int res=-1;
    for(int i=0;i<=7;i++)
    {
        string a=s;
        int cur=0;
        for(int j=1;j<=i;j++)
        {
            bool found=false;
            for(int k=j;k<=n;k++)
            {
                if(a[k]==t[j])
                {
                    found=true;
                    for(int z=k;z>j;z--)
                    {
                        cur++;
                        swap(a[z],a[z-1]);
                    }
                    break;
                }
            }
            if(!found) cur=1e9;
            //if(i==7) cout<<j<<' '<<found<<' ';
        }
        bool have=false;
        if(i!=7)
        {
            for(int k=i+1;k<=n;k++)
        {
            if(a[k]>t[i+1])
            {
                have=true;
                for(int z=k;z>i+1;z--)
                {
                    cur++;
                    swap(a[z],a[z-1]);
                }
                break;
            }
        }
        }
        if(cur==1e9) continue;
        if(have||(i==7&&n>7))
        {
            if(res==-1||res>cur) res=cur;
        }
    }
    cout<<res<<'\n';
}

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
    int t;
    cin>>t;
    while(t--)
        solve();
}
