#include <bits/stdc++.h>
#include <stdlib.h>
#include <fstream>
#include <iostream>
using namespace std;
typedef long long ll;
typedef pair<int,int> P;
typedef pair<ll,ll> llP;

ll mod(ll a, ll b){
  ll ret=a%b;
  if(ret<0) ret+=b;
  return ret;
}

ll modpow(ll a,ll b,ll c){
  ll res=1;
  while(b>0){
    if(b&1) res=mod(res*a,c);
    a=mod(a*a,c);
    b>>=1;
  }
  return res;
}

int GCD(int x,int y){
  if(x<y){
    swap(x,y);
  }
  while(x%y!=0){
    int temp=x;
    x=y;
    y=temp%y;
  }
  return y;
}

int digitsum(int x){
  int sum=0;
  while(x>0){
    sum+=(x%10);
    x/=10;
  }
  return sum;
}

int main() {
  int t;
  cin>>t;
  for(int I=0;I<t;I++){
    string s;
    cin>>s;
    string temp=s;
    sort(temp.begin(),temp.end());
    reverse(temp.begin(),temp.end());
    if(temp<="atcoder"){
      cout<<-1<<endl;
      continue;
    }
    string b="atcoder";
    if(b<s){
      cout<<0<<endl;
      continue;
    }
    for(int i=0;i<s.size();i++){
      if(s[i]!='a'){
        if(s[i]<='t'){
          cout<<i<<endl;
        }
        else{
          cout<<i-1<<endl;
        }
        break;
      }
    }
  }
  return 0;
}

#include <bits/stdc++.h>
#define FAST ios_base::sync_with_stdio(false);cin.tie(nullptr);
#define ll long long
#define l long int
#define f(i, a, b) for(int i=a; i<b; i++)
#define fr(i, a, b) for(int i=a; i>=b; i--)
#define endl '\n'
#define pb push_back
#define pf pop.front()
#define all(x) x.begin(),x.end()
#define debug(x) cerr << #x << " = " << x << endl;
#define lcm(a,b) a*b/__gcd(a,b)


using namespace std;
string s = "atcoder";


int solve()
{

string s1;
cin>>s1;

bool tr = false;
int n = s1.length();
int ans = 0;

for(int i=0;i<n;i++){
	if(s1[i]!= 'a')
	{
		tr = true;
	}
}

if(!tr )
return -1;

if(s < s1)
return 0;
int i;
for( i=0;i<n;i++){
	if(s1[i] != 'a')
	{
		break;
	}
}
if(s1[i] > 't')
		return i-1;
		
		return i;
}


int32_t main()
{
FAST
l T=1;
cin>>T;
while(T--)
{
cout<<solve()<<endl;
}
}








