#include<bits/stdc++.h>
#include<ext/pb_ds/assoc_container.hpp>
using namespace std;
using namespace __gnu_pbds;
 
typedef tree<int,null_type,less_equal<int>,rb_tree_tag,tree_order_statistics_node_update> order_set;
typedef long long  ll ;
 
 
 
#define     rep(i, begin, end)          for (__typeof(end) i = (begin) - ((begin) > (end)); i != (end) - ((begin) > (end)); i += 1 - 2 * ((begin) > (end)))
#define     what_is(x)                  cout << #x << " is " << x << endl;
// #define     endl                        '\n'
#define     fi                          first
#define     sec                         second
#define     pb                          push_back
#define     vi                          vector<int>
#define     vii                         vector<pair<int,int>>     
#define 	fio 						ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
#define 	sz(x) 						(int)x.size()
#define 	all(x) 						begin(x), end(x)
void solve(){
    string str;
    cin>>str;
    string temp=str;
    sort(temp.rbegin(),temp.rend());
    string init="atcoder";
    if(temp<=init){cout<<-1<<endl;return;}
    if(str>init){cout<<0<<endl;return;}
    int ln=sz(str);
    int pos;
    for(int i=0;i<ln;i++){
        if(str[i]!='a'){pos=i;break;}
    }
    if(str[pos]>'t'){cout<<pos-1<<endl;return;}
    cout<<pos<<endl;return;



    

}
int main(){
    fio;   
    int test;
    cin>>test;
    // test=1;
    while(test--){
        solve();
    }
    return 0;
}
 
 
#include<bits/stdc++.h>
using namespace std;
#define ll long long
int check(string &s)
{
    string ss="atcoder";
    if(s>ss)
    return 0;
    for(int i=1;i<s.size();i++)
    {
        if(s[i]>'t')
        return i-1;
        if(s[i]>'a')
        return i;
    }
    return -1;
}
int main()
{
    int t;
    cin>>t;
    while(t--)
    {
        string a;
        cin>>a;
        cout<<check(a)<<endl;
    }
    return 0;
}
