#include<iostream>
#include<string>
#include<vector>
#include<algorithm>
#include<bitset>
#include<set>
#include<map>
#include<stack>
#include<queue>
#include<deque>
#include<list>
#include<iomanip>
#include<cmath>
#include<cstring>
#include<functional>
#include<cstdio>
#include<cstdlib>
#include<numeric>

using namespace std;

#define repr(i, a, b) for (int i = (int)(a); i < (int)(b); i++)
#define rep(i, n) repr(i, 0, n)
#define INF 2e9
#define MOD 1000000007
//#define MOD 998244353
#define LINF (long long)4e18
#define jck 3.141592
#define PI acos(-1.0)

const double EPS = 1e-10;

using ll = long long;
using Pi = pair<int,int>;
using Pl = pair<ll,ll>;


int main(){
    int T; cin >> T;
    while(T--) {
        string s; cin >> s;
        int n = s.size();
        string t = "atcoder";
        if(s > t){
            cout << 0 << endl;
            continue;
        }
        bool ok = false;
        rep(i,n) {
            if(s[i] != 'a') ok = true;
        }
        if(!ok){
            cout << -1 << endl;
            continue;
        }
        int ans = INF;
        repr(i,1,n){
            if(s[i] > 't') {
                ans = min(ans,i-1);
            }
            else if(s[i] != 'a') {
                ans = min(ans,i);
            }
        }
        cout << ans << endl;
    }
}


#include <bits/stdc++.h>
using namespace std;
#define ll long long int
#define M 1000000007
#define pii pair<ll, ll>
#define prdb(pranay) cout << fixed << setprecision(10) << pranay;
#define speed1 ios_base ::sync_with_stdio(false);
#define speed2 cin.tie(NULL);
#define speed3 cout.tie(NULL);

int main()
{
#ifndef ONLINE_JUDGE
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
#endif
    speed1;
    speed2;
    speed3;

    ll t = 1;
    // cin >> t;
    while (t--)
    {
        ll n;
        cin >> n;
        vector<ll> v(n);
        ll i, j;
        ll gcd;
        for (i = 0; i < n; i++)
            cin >> v[i];
        sort(v.begin(), v.end(), greater<ll>());
        gcd = v.back();
        for (i = 0; i < n - 1; i++)
            gcd = __gcd(gcd, v[i]);

        cout << gcd;
    }
}