#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define ALL(v) (v).begin(), (v).end()
using ll = long long;
constexpr int INF = 1e9;
constexpr long long LINF = 1e18;
constexpr long long MOD = 1e9 + 7;

string a = "atcoder";

signed main() {
    int number_of_testcases;
    cin >> number_of_testcases;
    while (number_of_testcases--) {
        string s;
        cin >> s;
        int n = s.size();
        int cnt = 0;
        int ans = INF;
        rep(i, n) {
            if (i > 6) {
                ans = min(ans, cnt);
                break;
            }
            if (s[i] > a[i]) {
                ans = min(ans, cnt);
                break;
            }
            for (int j = i + 1; j < n; j++) {
                if (s[j] > a[i]) {
                    ans = min(ans, cnt + j - i);
                    break;
                }
            }
            if (s[i] < a[i]) {
                int pos = -1;
                for (int j = i + 1; j < n; j++) {
                    if (s[j] == a[i]) {
                        pos = j;
                        break;
                    }
                }
                if (pos == -1) break;
                for (int j = pos - 1; j >= i; j--) {
                    swap(s[j], s[j + 1]);
                    cnt++;
                }
            }
        }
        cout << (ans != INF ? ans : -1) << endl;
    }
    return 0;
}
#include<bits/stdc++.h>
#define endl '\n'
using namespace std;
string s;
int solve(){
    if(s>"atcoder")return 0;
    int id = -1;
    for(int i=0;i<s.size();++i){
        if(s[i]!='a'){
            id = i;
            break;
        }
    }
    if(id==-1)return id;
    if(s[id]>'t')return id - 1;
    return id;
}
int main(){
    int tt;
    cin>>tt;
    while(tt--){
        cin>>s;
        cout<<solve()<<endl;
    }
}
