#include<bits/stdc++.h>
#define rep(i,a,b) for(int i=(a);i<(b);++i)
#define per(i,a,b) for(int i=(b)-1;i>=(a);--i)
using namespace std;
const int N=1e3+9;
const int Inf=1e9;
char s[N],t[20]={"atcoder"};
int main(){
	int T;
	scanf("%d",&T);
	rep(Tcase,1,T+1){
		scanf("%s",s);
		int n=strlen(s);
		int ans=Inf,sum=0;
		rep(i,0,min(n,8)){
			//cout<<i<<' '<<ans<<"check\n";
			if(i==7){
				ans=min(ans,sum);
				break;
			}
			rep(j,i,n){
				if(s[j]>t[i]){
					ans=min(ans,sum+j-i);
					break; 
				}
			}
			int ok=0;
			rep(j,i,n){
				if(s[j]==t[i]){
					ok=1;
					while(j>i)swap(s[j],s[j-1]),sum++,--j;
					break;
				}
			}
			if(sum>ans||!ok)break;
		}
		if(ans==Inf)ans=-1;
		printf("%d\n",ans);
	}
}
#include<bits/stdc++.h>
using namespace std;

using d = int; 
using ll = long long;
using ud = unsigned int;
using ull = unsigned long long;
using lf = double;
using llf = long double;
using ch = char;
using st = string;
using bl = bool;
using vo = void;

#define ci cin
#define co cout

#define fi( x, n ) for( d i = x; i < n; i++ )
#define fj( x, n ) for( d j = x; j < n; j++ )
#define fin( x, n ) for( d i = x; i <= n; i++ )
#define fjn( x, n ) for( d j = x; j <= n; j++ )
#define wh while
#define w( n ) while( n-- )

#define vd vector<d>
#define vll vector<ll>
#define vlf vector<lf>
#define vc vector<ch>
#define vs vector<st>
#define sd set<d>
#define sll set<ll>
#define slf set<lf>
#define sc set<ch>
#define ss set<st>

#define sz(x) x.size()
#define st(x) sort( x.begin(), x.end() )
#define cou(x, y) count( x.begin(), x.end(), y ) 

#define in insert
#define ct count
#define fd find
#define pb push_back
#define er erase
#define be begin
#define en end
#define np next_permutation

#define el endl


vo InputOutput()
{
	#ifndef ONLINE_JUDGE
	freopen("input.txt", "r", stdin);
	freopen("output.txt", "w", stdout);
	#endif
}


#define SIZE 100000
bl check[SIZE];
vd prime;


void sieve( d number )  // O( n * log(log(n)) )
{
    d i, j;

    check[0] = check[1] = 1;

    for( i = 2; i*i <= number; i++ )
    {
        if( check[i] == 0 )
        {
            for( j = i*i; j <= number; j += i )
                check[j] = 1;
        }
    }

    for( i = 2; i <= number; i++ )
    {
        if( check[i] == 0 )
            prime.pb( i );
    }
}


d IsPrime( d num )  // O( n / 2 )
{
	if( num <= 2 )
        return 0;

    if( num % 2 == 0 )
        return 0;
    else
    {
        for( d i = 3; i*i <= num; i += 2 )
        {
            if( num % i == 0 )
                return 0;
        }

        return 1;
    }
}


d main()
{
	InputOutput();

	d x;

    ci >> x;

    d ans = ( (x / 100) + 1 ) * 100;

    ans -= x;

    co << ans << el;

	return 0;
}