#include <iostream>
#include <algorithm>
#include <string>
#include <vector>
#include <cmath>
#include <map>
#include <queue>
#include <iomanip>
#include <set>
#include <tuple>
#define mkp make_pair
#define mkt make_tuple
#define rep(i,n) for(int i = 0; i < (n); ++i)
#define all(v) v.begin(),v.end()
using namespace std;
typedef long long ll;
const ll MOD=1e9+7;
template<class T> void chmin(T &a,const T &b){if(a>b) a=b;}
template<class T> void chmax(T &a,const T &b){if(a<b) a=b;}

string P="atcoder";

void solve(){
  string S;
  cin>>S;
  int N=S.size();

  ll ans=MOD;
  ll sum=0;
  for(int k=0;k<=P.size();k++){
    if(k==P.size()){
      if(S.size()) chmin(ans,sum);
      break;
    }

    int pos=-1;
    for(int i=0;i<S.size();i++){
      if(S[i]>P[k]){
        pos=i;
        break;
      }
    }

    if(pos!=-1){
      ll res=sum+pos;
      chmin(ans,res);
    }

    int tar=-1;
    for(int i=0;i<S.size();i++){
      if(S[i]==P[k]){
        tar=i;
        break;
      }
    }
    if(tar==-1) break;
    sum+=tar;
    S.erase(S.begin()+tar);
  }

  if(ans==MOD) cout<<-1<<"\n";
  else cout<<ans<<"\n";
}

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

  int T;
  cin>>T;
  rep(i,T) solve();

  return 0;
}

#include <algorithm>
#include <bitset>
#include <cassert>
#include <cctype>
#include <cmath>
#include <cstddef>
#include <cstdint>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <utility>
#include <vector>
#define PB push_back
#define MP make_pair
#define YES cout << "YES" << endl
#define Yes cout << "Yes" << endl
#define NO cout << "NO" << endl
#define No cout << "No" << endl
#define INF (1 << 28)
#define LLINF (1LL << 60)
//#define MOD 998244353
#define MOD 1000000007
#define rep(i, n) for (int i = 0; i < n; i++)
using ll = long long;
using namespace std;
typedef pair<int, int> P;

int main()
{
    ll N;
    vector<ll> three, five;

    ll t = 1;
    while(t*3<=1000000000000000000){
        t *= 3;
        three.push_back(t);
    }
    t = 1;
    while (t * 5 <= 1000000000000000000)
    {
        t *= 5;
        five.push_back(t);
    }

    cin >> N;

    for (int i = 0; i < three.size();i++){
        for (int j = 0; j < five.size();j++){
            if(three[i]+five[j]==N){
                cout << i + 1 << ' ' << j + 1 << endl;
                return 0;
            }
        }
    }

    cout << -1 << endl;

    return 0;
}
