#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn = 200 + 10;

int c[8];

int main(){
	ios_base::sync_with_stdio(false);
	int tc;
	cin >> tc;
	while (tc --){
		string s;
		cin >> s;
		if (s > "atcoder"){
			cout << 0 << '\n';
			continue;
		}
		bool found = 0;
		for (int i = 0; i < s.size(); i++){
			if (s[i] > 'a'){
				if (s[i] > 't')
					cout << max(0,i-1) << '\n';
				else
					cout << i << '\n';
				found = 1;
				break;
			}
		}
		if (!found)
			cout << -1 << '\n';
	}
}

#include <bits/stdc++.h>

using namespace std;

string s;

string x = "atcoder";

int aduEgal(int poz)
{
    if(s[poz] == x[poz])
        return 0;

    for(int i = poz + 1; i < s.size(); i ++)
    {
        if(x[poz] == s[i])
        {
            for(int j = i; j > poz; j --)
                s[j] = s[j - 1];

            s[poz] = x[poz];

            return i - poz;
        }
    }

    return 9999;
}

int aduMare(int poz)
{
    if(s[poz] > x[poz])
        return 0;

    for(int i = poz + 1; i < s.size(); i ++)
    {
        if(s[i] > x[poz])
            return i - poz;
    }

    return 9999;
}

int main()
{
    int q;
    cin >> q;
    while(q > 0)
    {
        q --;

        cin >> s;

        int rez = 999999999;

        int ok = 0;
        for(int i = 0; i < s.size(); i ++)
        {
            if(s[i] != 'a')
                ok = 1;
        }

        if(ok == 0)
            cout << "-1\n";

        else
        {
            int cost = 0;
            for(int i = 0; i < min(s.size(), x.size()); i ++)
            {
                int w = aduMare(i);
                rez = min(rez, cost + w);

                cost += aduEgal(i);
            }

            if(s.size() > x.size())
                rez = min(rez, cost);

            cout << rez << "\n";
        }
    }

    return 0;
}
