#include "iostream"
#include "algorithm"
#include "cstring"
#include "cstdio"
#include "cmath"
#include "vector"
#include "map"
#include "set"
#include "queue"
using namespace std;
#define MAXN 2006
//#define int long long
#define rep(i, a, b) for (int i = (a), i##end = (b); i <= i##end; ++i)
#define per(i, a, b) for (int i = (a), i##end = (b); i >= i##end; --i)
#define pii pair<int,int>
#define fi first
#define se second
#define mp make_pair
#define pb push_back
#define eb emplace_back
#define vi vector<int>
#define all(x) (x).begin() , (x).end()
#define mem( a ) memset( a , 0 , sizeof a )
typedef long long ll;
int n , m;
int A[MAXN];
char ch[MAXN];
void solve() {
	scanf("%s",ch + 1);
	n = strlen( ch + 1 );
	string r = ch + 1;
	if( r > "atcoder" ) return puts("0") , void();
	int res = -1;
	rep( i , 1 , n ) if( ch[i] != 'a' ) {
		if( ch[i] > 't' && i > 2 ) res = i - 2;
		else res = i - 1;
		if( ~res ) break;
	}
	cout << res << endl;
}

signed main() {
    int T;cin >> T;while( T-- ) solve();
//    solve();
}


#include <bits/stdc++.h>

using namespace std;

typedef long long i64;
typedef i64 int_t;
typedef vector<int_t> vi;
typedef vector<vi> vvi;

typedef pair<int_t, int_t> pi;

#define tr(c, i) for(auto i = (c).begin(); i != (c).end(); i++)
#define pb push_back
#define sz(a) i64((a).size())
#define all(c) (c).begin(), (c).end()
#define REP(s, e, i) for(i=(s); i < (e); ++i)

int main(int argc, char *argv[]) {
  i64 T, t;
  cin >> T;
  const string Q = "atcoder";
  REP(0, T, t) {
    i64 i, c;
    string S;
    cin >> S;
    
    vvi pos(26);
    REP(0, sz(S), i) {
      i64 c = S[i] - 'a';
      pos[c].pb(i);
    }

    if(pos[0].size() == S.size()) {
      cout << -1 << endl;
      continue;
    }

    const i64 MAX = 100000000;
    i64 ans = MAX;
    bool eq = true;
    REP(0, sz(Q), i) {
      if(i >= sz(S)) {
	break;
      }
      
      if(S[i] > Q[i] && eq) {
	ans = 0;
	break;
      }

      if(eq) {
	REP(Q[i]-'a'+1, 26, c) {
	  if(!pos[c].empty()) {
	    auto lb = lower_bound(all(pos[c]), i);
	    if(lb != pos[c].end()) {
	      ans = min(ans, (*lb) - i);
	    }
	  }
	}
      }
      
      if(S[i] != Q[i]) {
	eq = false;
      }

    }

    if(sz(S) > sz(Q) && eq) {
      ans = 0;
    }

    if(ans == MAX) {
      ans = -1;
    }
    
    cout << ans << endl;
  }

  return 0;
}
