#include <bits/stdc++.h>
#define ll long long
#define pb push_back
#define mp make_pair

using namespace std;

int t;

void solve()
{
    cin >> t;

    while(t--)
    {
        string s;

        cin >> s;

        if(s > "atcoder")
        {
            cout << 0 << "\n";
            continue;
        }

        if(s == string(s.size(), 'a'))
        {
            cout << -1 << "\n";
            continue;
        }

        for(int i = 1; i < (int) s.size(); i++)
        {
            if(s[i] != 'a')
            {
                cout << i - (s[i] > 't' ? 1 : 0) << "\n";
                break;
            }
        }
    }
}


int main()
{   
    ios::sync_with_stdio(false);
    cin.tie(0);

    solve();
    
    return 0;
}       
#include<stdio.h>
#include<iostream>
#include<algorithm>
#include<vector>
#include<string>
#include <cassert>
#include <numeric>
#include <unordered_map>
#include <queue>
#include <math.h>
#include <climits>
#include <set>
#include <map>
using namespace std;

using ll = long long;
using ld = long double;
using pll = pair<ll, ll>;
using pdd = pair<ld, ld>;
//template<class T> using pq = priority_queue<T, vector<T>, greater<T>>;

#define FOR(i, a, b) for(ll i=(a); i<(b);i++)
#define REP(i, n) for(ll i=0; i<(n);i++)
#define ROF(i, a, b) for(ll i=(b-1); i>=(a);i--)
#define PER(i, n) for(ll i=n-1; i>=0;i--)
#define REPREP(i,j,a,b) for(ll i=0;i<a;i++)for(ll j=0;j<b;j++)
#define VV(type) vector< vector<type> >
#define VV2(type,n,m,val) vector< vector<type> > val;val.resize(n);for(ll i;i<n;i++)val[i].resize(m)
#define vec(type) vector<type>
#define VEC(type,n,val) vector<type> val;val.resize(n)
#define VL vector<ll>
#define VVL vector< vector<ll> >
#define SZ size()
#define all(i) begin(i),end(i)
#define SORT(i) sort(all(i))
#define BITI(i) (1<<i)
#define BITSET(x,i) x | (1<<i)
#define BITCUT(x,i) x & ~(1<<i)
#define EXISTBIT(x,i) ((x>>i) & 1 != 0)
#define ALLBIT(n) (1<<n-1)



int main() {
  string s;
  cin >> s;
  REP(i, s.size()-1) {
    cout << s[i+1];
  }
  cout << s[0];
  return 0;
}
