#include<bits/stdc++.h>
using namespace std;
#define X first
#define Y second
#define sz(a) (int)a.size()
#define ll long long

signed main()
{
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    int n;
    cin >> n;
    while(n--)
    {
        string s;
        cin >> s;
        string s1 = "atcoder";
        int ans1 = 1e9;
        for(int i = 0; i <= min(7, sz(s)); i++)
        {
            vector <bool> used(sz(s));
            int j = 0;
            int ans = 0;
            while(j < i)
            {
                bool fl = 0;
                for(int p = 0; p < sz(s); p++)
                {
                    if(s1[j] == s[p] && !used[p])
                    {
                        used[p] = 1;
                        fl = 1;
                        break;
                    }
                    if(!used[p])
                    {
                        ans++;
                    }
                }
                if(!fl)
                {
                    break;
                }
                j++;
            }
            if(j < i)
            {
                continue;
            }
            if(i == 7)
            {
                if(i != sz(s))
                {
                ans1 = min(ans1, ans);
                }
            }
            else
            {
                bool fl = 0;
                for(int p = 0; p < sz(s); p++)
                {
                    if(!used[p] && s[p] > s1[i])
                    {
                        fl = 1;
                        break;
                    }
                    if(!used[p])
                    {
                        ans++;
                    }
                }
                if(fl)
                {
                    ans1 = min(ans1, ans);
                }
            }
        }
        if(ans1 == 1e9)
        {
            ans1 = -1;
        }
        cout << ans1 << "\n";
    }
    return 0;
}

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

int main(){
    int T;
    cin >> T;
    for(int i = 0; i < T; i++){
        string S;
        cin >> S;
        int rest = 0;
        string atcoder = "atcoder";
        int Slength = S.length();
        for(int j = 0; j < min(7, Slength); j++){
            rest += (S[j] - atcoder[j]) * 26 * (min(7, Slength) - j);
        }
        if(rest > 0 || (rest == 0 && Slength > 7)){
            cout << 0 << endl;
        }else{
            bool no = true;
            for(int j = 0; j < Slength; j++){
                if(S[j] != 'a'){
                    no = false;
                    if(S[j] > 116){
                        cout << j - 1 << endl;
                    }else{
                        cout << j << endl;
                    }
                    break;
                }
            }
            if(no){
                cout << -1 << endl;
            } 
        }
    }
}