#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>

using namespace std;

string at="atcoder";

void solve()
{
    string s;
    cin>>s;

    if(at<s) return puts("0"),(void)0;

    int pos=-1;
    for(int i=1;i<s.length();i++)
    {
        if(s[i]!='a')
        {
            pos=i;
            break;
        }
    }

    if(pos==-1) puts("-1");
    else if(s[pos]>'t') printf("%d\n",pos-1);
    else printf("%d\n",pos);
}

int main()
{
    int T;
    scanf("%d",&T);
    while(T--) solve();
    return 0;
}
#define _CRT_SECURE_NO_WARNINGS
#include <bits/stdc++.h>
using namespace std;

int main() {
	ios::sync_with_stdio(false);
	cin.tie(nullptr);
	cout.tie(nullptr);
	int t; cin >> t;
	string b = "atcoder";
	while (t--) {
		string s;
		cin >> s;
		if (s > b) cout << "0\n";
		else {
			vector<char> ch;
			for (auto c : s)
				ch.push_back(c);
			sort(ch.begin(), ch.end(), greater<char>());
			string a;
			for (auto c : ch)
				a.push_back(c);
			if (a <= b) cout << "-1\n";
			else {
				if (s[1] == 'a') {
					int ans = INT_MAX;
					for (int i = 2; i < s.length(); i++) {
						if (s[i] != 'a') ans = min(ans, i);
						if (s[i] > 't') ans = min(ans, i - 1);
					}
					cout << ans << '\n';
				}
				else cout << "1\n";
			}
		}
	}
	return 0;
}