#include <cstdio>
#include <iostream>
#include <algorithm>
#include <cstring>
#include <cmath>
#include <bitset>
#include <vector>
using namespace std;
 
const int N = 1e5;


int main() {
    #ifndef ONLINE_JUDGE
    freopen("in.txt", "r", stdin);
    #endif
    int t;
    cin >> t;
    string atcoder = "atcoder";
    for (int tests = 0; tests < t; ++tests) {
        string s;
        cin >> s;
        if (s > atcoder) {
            puts("0");
            continue;
        }

        bool ok = false;
        int ans = 1e9;
        for (int i = 0; i < s.length(); ++i) {
            if (s[i] > 'a') {
                ok = true;
                ans = min(ans, i);
            }
            if (s[i] > 't') {
                ans = min(ans, i - 1);
            }
        }
        if (!ok) {
            puts("-1");
            continue;
        }
        cout << ans << endl;
    }
    return 0;
}
#include <bits/stdc++.h>
using namespace std;

int main() {
    int t;
    cin >> t;
    while (t--) {
        string s;
        cin >> s;
        string at = "atcoder";
        int ans = 1e9;
        int acc = 0;
        for (int i = 0; i < at.size(); ++i) {
            int posx = -1, posy = -1;
            for (int j = i; j < s.size(); ++j) {
                if (s[j] == at[i] && posx == -1) posx = j;
                if (s[j] > at[i] && posy == -1) posy = j;
            }
            if (posy != -1) ans = min(ans, acc + posy - i);
            if (posx == -1) break;
            while (posx != i) {
                acc += 1;
                swap(s[posx], s[posx-1]);
                posx -= 1;
            }
        }
        if (at < s) ans = min(ans, acc);
        if (ans < 1e7) cout << ans << endl;
        else cout << -1 << endl;
    }
    return 0;
}