#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); ++i)
using namespace std;
using ll = long long;
const ll INF = ll(1e18) + 5;
string atcoder = "atcoder";

void solve() {
    string S;
    cin >> S;
    if (S > atcoder) {
        cout << 0 << endl;
        return;
    }
    string revS = S;
    sort(revS.rbegin(), revS.rend());
    if (atcoder >= revS) {
        cout << -1 << endl;
        return;
    }
    int n = S.size();
    ll ans = n;
    // Sの中でaより大きな最初のindexを求める.
    rep(i, n) {
        if (S[i] > 'a') {
            ans = i;
            break;
        }
    }
    // Sの中でtより大きな最初のindexを求める.
    for (int i = 2; i < n; i++) {
        if (S[i] > 't') {
            ans = min(ans, i - 1ll);
            break;
        }
        if (S[i] == 't') {
            ans = min(ans, (ll)i);
        }
    }
    cout << ans << endl;
}

int main() {
    int T;
    cin >> T;
    rep(i, T) {
        solve();
    }

    return 0;
}
#include<bits/stdc++.h>
#define ll long long int
using namespace std;
ll n,low,high,mid,x,z,a;
void solve(ll the)
{
    ll i,j,k,b,c,d,m,l,r,sum=0,ans=0,temp,t;
    scanf("%lld",&n);a=n+1;
    low=0;high=sqrt((8*a)+1)-1;high/=2;
    while(low<=high){
    	mid=low+(high-low)/2;
    	x=1;
    	if(mid&1){
    		x=(mid+1)/2;x*=mid;
    	}
    	else{
    		x=mid/2;x*=(mid+1);
    	}
    	if(x<=a){
    		z=mid;
    		low=mid+1;
    	}
    	else{
    		high=mid-1;
    	}
	   // printf("%lld\n",x);
    }
    ans=1+(n-z);
    //printf("%lld\n",z);
    //cout << "Hello" << endl;
    cout << ans;
    
    
}
int main()
{
	long long int t=1,c=1;
	// scanf("%lld",&t);
	while(t--){
		solve(c);
		c++;
	}
	return 0;
}