#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 <bits/stdc++.h>
using namespace std;

const long long mod = 1e9 + 7;

using ll = long long;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
#define ull unsigned long long
#define ld long double
#define vi vector<int>
#define vll vector<ll>
#define vc vector<char>
#define vs vector<string>
#define vpii vector<pii>
#define vpll vector<pll>

#define rep(i, n) for (int i = 0, i##_len = (n); i < i##_len; i++)
#define rep1(i, n) for (int i = 1, i##_len = (n); i <= i##_len; i++)
#define repr(i, n) for (int i = ((int)(n)-1); i >= 0; i--)
#define rep1r(i, n) for (int i = ((int)(n)); i >= 1; i--)

#define sz(x) ((int)(x).size())
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()

#define SORT(v, n) sort(v, v + n);
#define VSORT(v) sort(all(v));
#define RSORT(v) sort(rall(v));
#define pb push_back
#define mp make_pair

#define INF (1e9)
#define PI (acos(-1))
#define EPS (1e-7)

ull gcd(ull a, ull b) { return b ? gcd(b, a % b) : a; }
ull lcm(ull a, ull b) { return a / gcd(a, b) * b; }

ll comb(int n, int r) {
  if (r * 2 > n) r = n - r;
  ll ans = 1;
  for (int i = 1; i <= r; ++i) {
    ans *= n - i + 1;
    ans /= i;
  }
  return ans;
}

void solve(long long L) { cout << comb(L - 1, 11) << endl; }

int main() {
  // cout << fixed << setprecision(5);

  long long L;
  scanf("%lld", &L);
  solve(L);
  return 0;
}
