#include <iostream>
#include <cmath>
#include <string>
#include <vector>
#include <algorithm>
#include <utility>
#include <tuple>
#include <cstdint>
#include <cstdio>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <deque>
#include <unordered_map>
#include <unordered_set>
#include <bitset>
#include <cctype>
#include <climits>

#define rep(i, n) for(int i = 0; i < n; i++)
#define per(i, n) for(int i = n - 1; i >= 0; i--)
using ll = long long;
using ld = long double;
#define vi vector<int>
#define vvi vector<vi>
#define vl vector<ll>
#define pii pair<int, int>
#define pll pair<ll, ll>
#define all(a) (a).begin(), (a).end()
#define rall(a) (a).rbegin(), (a).rend()
#define mod 1000000007
using namespace std;
void solve(){
  string s;
  cin >> s;
  int n = s.size();
  if(s > "atcoder"){
    cout << "0\n";
    return;
  }
  rep(i, n) if(s[i] != 'a'){
    if(s[i] > 't') cout << max(i - 1, 0) << "\n";
    else cout << i << "\n";
    return;
  }
  cout << "-1\n";
}
int main(){
  int t;
  cin >> t;
  rep(i, t) solve();
}
/// kazuki08
/*
#pragma comment(linker, "/stack:200000000")
#pragma GCC optimize("O3")
#pragma GCC optimize("Ofast")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#pragma GCC optimize("unroll-loops")
*/
#include<bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
using namespace std;
#define int long long
typedef long long ll;
typedef tree<int,null_type,less<int>,rb_tree_tag,tree_order_statistics_node_update>ordered_set;
typedef long double ld;
typedef vector<int> vi;
typedef pair<int,int> pi;
typedef vector<pi> vpi;
#define ar array
//#define inf 1000000000
#define mod 1000000007
#define dmp(x) cerr<<"line "<<__LINE__<<" "<<#x<<":"<<x<<endl
#define fs first
#define sc second
#define pb push_back
#define all(c) (c).begin(),(c).end()
#define mt make_tuple
#define IOS ios::sync_with_stdio(0);cin.tie(0);cout.tie(0)
template<class S,class T> ostream& operator<<(ostream& o,const pair<S,T> &p){
    return o<<"("<<p.fs<<","<<p.sc<<")";
}
template<class T> ostream& operator<<(ostream& o,const vector<T> &vc){
    o<<"{";
    for(const T& v:vc) o<<v<<",";
    o<<"}";
    return o;
}
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());

string s,t="atcoder";
int n;

int match(int x){
  if(x==0){
    int res = mod;
    for(int j=0;j<s.size();j++){
      if(s[j]>'a')return j;
    }
    return res;
  }
  int done[n+1]={0};
  int res = 0;
  for(int i=0;i<x;i++){
    int cnt = 0;
    bool f = false;
    for(int j=0;j<s.size();j++){
      if(done[j])continue;
      if(s[j]==t[i]){
        done[j]=1;
        res+=cnt;
        f = true;
        break;
      }
      cnt++;
    }
    if(!f)return mod;
  }
  if(x<t.size()){
    bool f = false;
    int cnt = 0;
    for(int j=0;j<s.size();j++){
      if(done[j])continue;
      if(t[x]<s[j]){
        res+=cnt;
        f = true;
        break;
      }
      cnt++;
    }
    if(!f)return mod;
  }
  else {
    if(s.size()>x)return res;
    else return mod;
  }
  return res;
}

signed main(){
IOS;
//freopen("input.txt", "r", stdin);
//freopen("output.txt", "w", stdout);
  
  int T;
  cin >> T;
  while(T--){
    cin >> s;
    n = s.size();
    int ans = mod;
    for(int i=0;i<=t.size();i++){
      ans = min(ans,match(i));
      // cerr << i << " "  << ans << endl;
    }
    if(ans>=mod){
      ans = -1;
    }
    cout << ans << endl;
  }


return 0;
}
///....
