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

template<typename T>
ostream& operator<<(ostream &os, vector<T> &v){
    string sep = " ";
    if(v.size()) os << v[0];
    for(int i=1; i<v.size(); i++) os << sep << v[i];
    return os;
}

template<typename T>
istream& operator>>(istream &is, vector<T> &v){
    for(int i=0; i<v.size(); i++) is >> v[i];
    return is;
}

#ifdef DBG
void debug_(){ cout << endl; }
template<typename T, typename... Args>
void debug_(T&& x, Args&&... xs){
    cout << x << " "; debug_(forward<Args>(xs)...);
}
#define dbg(...) debug_(__VA_ARGS__)
#else
#define dbg(...)
#endif

void solve(){
    string s; cin >> s;
    int ans = 1e9;
    int m = 0;
    for(auto c: "atcoder") {
        int x=-1, y=-1;
        for(int i=0; i<s.size(); i++){
            if(s[i]>=c+1){
                x=i;
                break;
            }
        }
        if(x>=0) ans = min(ans, x+m);
        for(int i=0; i<s.size(); i++){
            if(s[i]>=c){
                y=i;
                break;
            }
        }
        if(y>=0){
            m += y;
            s = s.substr(0, y) + s.substr(y+1);
        } else {
            break;
        }
    }

    if(ans==1e9) cout << -1 << endl;
    else cout << ans << endl;
}

int main() {
    ios_base::sync_with_stdio(false);
    cout << setprecision(20) << fixed;
    int t; cin >> t;
    for(int i=0; i<t; i++) solve();
    return 0;
}
#include <iostream>
#include <fstream>
#include <iomanip>
#include <cstdio>
#include <cstdlib>
#include <algorithm>
#include <bitset>
#include <set>
#include <map>
#include <vector>
#include <string>
#include <cstring>
#include <queue>
#include <memory.h>
#include <cmath>

using namespace std;

#define min(a,b) ((a)<(b)?(a):(b))
#define max(a,b) ((a)>(b)?(a):(b))
#define FOR0(i,n) for( i = 0 ; i < n ; ++i )
#define FOR1(i,n) for( i = 1 ; i <= n ; ++i )
#define sys_p system( "pause" )
#define pb push_back
#define mp make_pair
#define FI first
#define SE second
#define sz(a) (int)a.size()

typedef long long LL;

const LL MOD = (LL)(1e9 + 7);

int t, i, j, found[7], ans1, ans2;
string s, x = "atcoder";

int main() {
    cin >> t;

    while (t--) {
        cin >> s;

        FOR0(i, 7) {
            found[i] = -1;
        }

        ans1 = ans2 = 1e9;

        // option 1: atcoderXXX
        if (s.length() > 7) {
            FOR0(i, s.length()) {
                char c = s[i];
                FOR0(j, 7) {
                    if (c == x[j] && ((found[j] < 0) || abs(i - j) < abs(i - found[j]))) {
                        found[j] = i;
                    }
                }
            }

            ans1 = 0;
            FOR0(j, 7) {
                if (found[j] < 0) {
                    ans1 = 1e9;
                    break;
                }
                else {
                    ans1 += abs(found[j] - j);
                }
            }

        }

        // option 2: put larger letter somewhere
        int prev_cost = 0;
        FOR0(j, 7) {
            for (i = j; i < s.length(); ++i) {
                if (s[i] > x[j]) {
                    ans2 = min(ans2, prev_cost + i - j);
                    break;
                }
            }

            for (i = j; i < s.length(); ++i) {
                if (s[i] == x[j]) {
                    s.erase(i, 1);
                    s.insert(j, 1, x[j]);
                    prev_cost += i - j;
                    break;
                }
            }

            if (i == s.length()) {
                break;
            }
        }

        if (min(ans1, ans2) > 1e8) {
            cout << -1 << endl;
        }
        else {
            cout << min(ans1, ans2) << endl;
        }
    }

    return 0;
}
