/*
"An anomaly, I'm Muhammad Ali
  Cause I know one day I'm gonna be the"
                - Greatest, Eminem
*/
#pragma GCC optimize ("O3")
#pragma GCC target ("sse4")
#include<bits/stdc++.h>
#include<ext/pb_ds/assoc_container.hpp>
#include<ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;
typedef long long int ll;
#define ff first
#define Shazam ios_base::sync_with_stdio(false);   cin.tie(NULL); cout.tie(NULL);
#define ss second
#define all(c) c.begin(),c.end()
#define endl "\n"
#define test() int t; cin>>t; while(t--)
#define fl(i,a,b) for(int i = a ; i <b ;i++)
#define get(a) fl(i,0,a.size())  cin>>a[i];
#define pra(a) fl(i,0,a.size()) cout<<a[i]<<" ";	cout<<endl;
#define pr(a,n) fl(i,0,n) cout<<a[i]<<" ";	cout<<endl;
typedef tree<int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update> ordered_set;
const ll INF = 2e18;
const int inf = 2e9;
const int mod1 = 1e9 + 7;



int main(){
    Shazam;
    string s; cin >> s;
    while(s.size() && s.back() == '0') s.pop_back();
    for(int i = 0; i < s.size() / 2 ; i++){
        if(s[i] == s[s.size() - 1 - i]) continue;
        cout << "No" << endl;
        return 0;
    }
    cout << "Yes" << endl;
    return 0;
}
#include <bits/stdc++.h>
using namespace std;
const int N = 1e5+10;
char s[N],t[N],f[]=" atcoder";
int main() {
	int T;
	scanf("%d", &T);
	while (T--) {
		scanf("%s", s+1);
		int n = strlen(s+1);
		int ook = 0;
		for (int i=1; i<=min(n,7); ++i) {
			if (s[i]>f[i]) {
				ook = 1;
				break;
			}
			if (s[i]<f[i]) {
				ook = -1;
				break;
			}
		}
		if (ook==1||ook==0&&n>7) {
			puts("0");
			continue;
		}
		for (int i=1; i<=n; ++i) t[i] = s[i];
		sort(t+1,t+1+n,greater<char>());
		int ok = 0;
		for (int i=1; i<=min(n,7); ++i) {
			if (t[i]>f[i]) { 
				ok = 1;
				break;
			}
			if (t[i]<f[i]) { 
				ok = -1;
				break;
			}
		}
		if (ok==-1||ok==0&&n<7) puts("-1");
		else {
			int ans = 1e9;
			for (int i=1; i<=n; ++i) if (s[i]>'a') {
				ans = min(ans, abs(i-1));
			}
			for (int i=1; i<=n; ++i) if (s[i]>'t') {
				ans = min(ans, abs(i-2));
			}
			printf("%d\n", ans);
		}
	}
}


