#include <bits/stdc++.h>

using namespace std;

int main() {
    string s = "atcoder";
    int T;  cin >> T;
    while (T--) {
        string t;   cin >> t;
        int len = t.size();
        int ans = INT_MAX, sum = 0;
        for (auto cs : s) {
            int cnt = 0;
            for (auto it = t.begin(); it != t.end(); it++, cnt++) {
                if (*it > cs) {
                    ans = min(ans, sum + cnt);
                    break;
                }
            }
            cnt = 0;
            bool flag = false;
            for (auto it = t.begin(); it != t.end(); it++, cnt++) {
                if (*it > cs)   break;
                else if (*it == cs) {
                    flag = true;
                    sum += cnt;
                    t.erase(it);
                    break;
                }
            }
            if (!flag)  break;
        }
        if (t.size() && len - t.size() == s.size()) ans = min(ans, sum);
        printf("%d\n", ans == INT_MAX ? -1 : ans);
    }
    return 0;
}
#include <bits/stdc++.h>
#define rep(i,n) for(int i=0; i<(n); i++)
#define reps(i,s,n) for(int i=(s); i<(n); i++)
#define all(v) v.begin(),v.end()
#define outve(v) for(auto i : v) cout << i << " ";cout << endl
#define outmat(v) for(auto i : v){for(auto j : i) cout << j << " ";cout << endl;}
#define in(n,v) for(int i=0; i<(n); i++){cin >> v[i];}
#define out(n) cout << (n) << endl
#define fi first
#define se second
#define pb push_back
#define si(v) int(v.size())
#define len(v) int(v.length())
#define lob(v,n) lower_bound(all(v),n)
#define lobi(v,n) lower_bound(all(v),n) - v.begin()
#define upb(v,n) upper_bound(all(v),n)
#define upbi(v,n) upper_bound(all(v),n) - v.begin()
#define mod 1000000007
#define infi 1900000000
#define infl 1100000000000000000
#define cyes cout << "Yes" << endl
#define cno cout << "No" << endl
#define csp << " " <<
#define outset(n) cout << fixed << setprecision(n);
using namespace std;
using ll = long long;
using ld = long double;
using vi = vector<int>;
using vvi = vector<vector<int>>;
using vd = vector<double>;
using vvd = vector<vector<double>>;
using vl = vector<ll>;
using vvl = vector<vector<ll>>;
using pii = pair<int,int>;
using pll = pair<ll,ll>;
template<typename T> using ve = vector<T>;
template<typename T> using pq2 = priority_queue<T>;
template<typename T> using pq1 = priority_queue<T,vector<T>,greater<T>>;
template<typename T> bool chmax(T &a, T b) {if(a < b) {a = b;return 1;}return 0;}
template<typename T> bool chmin(T &a, T b) {if(a > b) {a = b;return 1;}return 0;}




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

    
    int T;
    cin >> T;
    
    vi ans;
    string a = "atcoder";
    rep(tt,T){
        string S;
        cin >> S;
        int an = 0;
        rep(i,len(a)){
            
            if(a[i] < S[i]) {
                an = 0;
                break;
            }else if(a[i] == S[i]){
                if(i == len(a)-1 && len(a) == len(S)) an = 1;
                if(i == len(S)-1 && i >= 1){
                    an = 1;
                    break;
                }
            }else{
                
                if(i >= 2) an = 1;
                else{
                    int aa = infi;
                    reps(j,i,len(S)){
                        if(S[j] > 't') {
                            aa = j-i;
                            break;
                        }
                    }
                    rep(j,len(S)){
                        if(S[j] > 'a') {
                            chmin(aa,j);
                            break;
                        }
                    }
                    
                    if(aa == infi) an = -1;
                    else an = aa;
                }
                
                break;
            }
        }
        
        ans.pb(an);
    }
    
    rep(i,T) cout << ans[i] << endl;

    return 0;
}
