#include<bits/stdc++.h>
#define int long long
using namespace std;
signed main(){
  int Q;
  cin>>Q;
  string ans="atcoder";
  while(Q--){
    string S;
    cin>>S;
    string T=S;
    int N=S.size();
    int mn=N*N;
    sort(T.rbegin(),T.rend());
    if(T<=ans){
      puts("-1");
      goto home;
    }
    if(S>ans){
      puts("0");
      goto home;
    }
    for(int i=min(6LL,N-1);i>=0;i--)
      if(S.substr(0,i)==ans.substr(0,i))
        for(int j=i+1;j<N;j++)
          if(S[j]>ans[i]){
            mn=min(mn,j-i);
          }
    cout<<mn<<endl;
    home:;
  }
}

// Problem : B - Permutation Check
// Contest : AtCoder - AtCoder Beginner Contest 205
// URL : https://atcoder.jp/contests/abc205/tasks/abc205_b
// Memory Limit : 1024 MB
// Time Limit : 2000 ms
// Powered by CP Editor (https://github.com/cpeditor/cpeditor)

#include <bits/stdc++.h>
using namespace std;
 
#define int   long long
#define FAST  ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
typedef pair<int,int>   pii;
typedef vector<int>     vi;
#define rep(i,s,e)      for(int i=(s);i<(e);i++)
#define pb              push_back
#define ff              first
#define ss              second
#define all(x)          (x).begin(), (x).end()
#define INF             (long long) 1e15
#define MOD             1000000007
#define MAX             (int) 2e5+5


void solve() {
    int n;
    cin >> n;
    int arr[n];
    rep(i,0,n) cin >> arr[i];
    sort(arr,arr+n);
    int c = 1;
    rep(i,0,n){
        if(c == arr[i]) c++;
        else {
            cout << "No\n";
            return;
        }
    } 
    cout << "Yes\n";
}

int32_t main(){
    FAST
    int t=1;
    // cin>>t;
    while(t--)
        solve();
    return 0;
}
