#include <iostream>
#include <vector>
#include <algorithm>
#include <string>
#include <cmath>
#include <numeric>
#include <queue>
using namespace std;

int64_t min(int64_t a,int64_t b) {
    if (a > b)
    {
        return b ;
    }else
    {
        return a ;
    }
}

int64_t gcd(int64_t a,int64_t b){
    if(a < b)
    {
        int64_t temp = a;
        a = b ;
        b = temp;
    }
    int64_t copya = a;
    int64_t copyb = b;
    int64_t r = a%b ;
    while (r != 0)
    {
        a = b ;
        b = r ;
        r = a%b ;
    }
    return b ;
    
}
int solve(string s, int start){
    if (start >= s.size())
    {
        return 100000000 ;
    }
    
    string at = "atcoder" ;
    if (start >= at.size())
    {
        return 0 ;
    }
    
    
    int l = 100000000 ;
    int same = 100000000 ;
    for (int i = start; i < s.size(); i++)
    {
        if (l == 100000000)
        {
            if (s.at(i) > at.at(start))
            {
                l = i -start ;
                break ;
            }
            
            
        }
        if (same == 100000000)
        {
            if (s.at(i) == at.at(start))
            {
                same = i - start ;
            }
            
        }
        
        
    }
    if (same != 100000000)
    {
        for (int i = 0; i < same; i++)
        {
            swap(s.at(same+start -i),s.at(same+start -i-1)) ;
        }
    }
    
    if(l != 100000000 && same != 100000000)
    {
        return min(l,solve(s,start+1)+same) ;
    }else if (same == 100000000)
    {
        return l  ;
    }else
    {
        return solve(s,start+1)+same ;
    }
    
    
    
    
    
}

int main(){
    string at = "atcoder" ;
    int t ;
    cin >> t ;
    for (int cases = 0; cases < t; cases++)
    {
        string s ;
        cin >> s ;
        string copy = s ;
        sort(copy.begin(),copy.end()) ;
        reverse(copy.begin(),copy.end()) ;
        if (at >= copy)
        {
            cout << -1 << endl ;
            continue ;
        }
        int ans = solve(s,0) ;
        
        
        
        cout << ans << endl ;
    }
    
}

#include <iostream>
#include <iomanip>
#include <vector>
#include <cmath>
#include <algorithm>
#include <deque>
#include <set>
#include <limits>
#include <string>
#include <map>
#include <unordered_map>
#include <unordered_set>
#include <list>
#include <numeric>
#include <bitset>

using namespace std;
using ll = long long;

#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define REP(i, a) for (int i = 0; i < (a); ++i)
#define OUT_PREC(a, n) cout << fixed << setprecision(n) << a << endl

class UnionFind
{
private:
    int n;
    struct NODE
    {
        ll val;
        int root = -1;
    };

    vector<NODE> nd;
    vector<int> size;

public:
    UnionFind(const int &n0)
    {
        n = n0;
        nd.resize(n);
        size.resize(n, 1);

        for (int i = 0; i < n; ++i)
        {
            nd[i] = NODE{i, -1};
        }
    }

    void merge(int a, int b)
    {
        int ra = find(a);
        int rb = find(b);

        if (ra == rb) return;

        if (size[ra] <= size[rb])
        {
            nd[ra].root = rb;
            size[rb] += size[ra];
        }
        else
        {
            nd[rb].root = ra;
            size[ra] += size[rb];
        }
    }

    int find(int a)
    {
        int r = nd[a].root;
        if (r == -1)
            return a;
        else
        {
            int t = find(r);
            nd[a].root = t;
            return t;
        }
    }

    bool same(int a, int b)
    {
        return find(a) == find(b);
    }
};

double solve()
{
    int n;
    cin >> n;
    vector<vector<ll>> d(n, vector<ll>(n + 2, -1LL));
    vector<ll> x(n), y(n);
    REP(i, n)
        cin >> x[i] >> y[i];

    FOR(i, 0, n)
    {
        FOR(j, i + 1, n)
        {
            d[i][j] = (x[i] - x[j]) * (x[i] - x[j]) + (y[i] - y[j]) * (y[i] - y[j]);
        }

        d[i][n] = (100 - y[i]) * (100 - y[i]);
        d[i][n + 1] = (100 + y[i]) * (100 + y[i]);
    }

    // 4r^2は整数
    ll ok = 0;
    ll ng = 40001;

    do
    {
        int m = (ok + ng) / 2;

        UnionFind uf = UnionFind(n + 2);
        // 要素n, n+1が壁
        FOR(i, 0, n){
            FOR(j, i + 1, n + 2){
                if (d[i][j] < m) uf.merge(i, j);
            }
        }

        (uf.same(n, n + 1) ? ng : ok) = m;

    } while (abs(ok - ng) > 1);

    return sqrt((double)ok) / (double)2.0;
}

int main()
{
    /* solve */
    cout << fixed << setprecision(15) << solve() << endl;
    // OUT((solve() ? "Yes" : "No"));
    system("pause");
}
