// #pragma GCC optimize ("O3")
#include <bits/stdc++.h>
using namespace std;

#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;

template <class T>
using ordered_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;

template <class key, class value, class cmp = std::less<key>>
using ordered_map = tree<key, value, cmp, rb_tree_tag, tree_order_statistics_node_update>;
// find_by_order(k)  returns iterator to kth element starting from 0;
// order_of_key(k) returns count of elements strictly smaller than k;

// Defines

#define pb push_back
#define ff first
#define ss second
#define quick                    \
    ios::sync_with_stdio(false); \
    cin.tie(0);
#define time cerr << (0.1 * clock()) / CLOCKS_PER_SEC << endl;
#define int long long
typedef pair<int, int> pl;
#define forn(n) for (int i = 0; i < n; i++)
#define endl "\n"
#define all(v) (v).begin(), (v).end()
#define ppc __builtin_popcount
#define ppcll __builtin_popcountll
int power(int a, int n, int md) {
    if (n == 0) {
        return 1;
    } else {
        int res = power(a, n / 2, md);
        res = (res * res) % md;
        if (n % 2 != 0) {
            res = (res * a) % md;
        }
        return res;
    }
}
random_device rndm;
mt19937 grndm(rndm());
void mix(int* a, int* b) { shuffle(a, b, grndm); }

// Constants

const int mod = 1000000007;

// Code begins

void solve() {
    string s;
    cin >> s;
    string t = "atcoder";
    if (t < s) {
        cout << 0 << '\n';
        return;
    }
    int ind = min(t.length(), s.length());
    vector<set<int>> v(26);
    for (int i = 0; i < s.length(); ++i) {
        v[s[i] - 'a'].insert(i);
    }
    int ans = 1e9, pre = 0;
    for (int i = 0; i < min(t.length(), s.length()); ++i) {
        v[s[i] - 'a'].erase(i);
        for (int j = t[i] - 'a' + 1; j < 26; ++j) {
            if (v[j].size()) {
                ans = min(ans, pre + *(v[j].begin()) - i);
            }
        }
        if (s[i] != t[i]) {
            if (v[t[i] - 'a'].size()) {
                pre += (*(v[t[i] - 'a'].begin()) - i);
            } else {
                break;
            }
        }
    }
    cout << (ans >= 1e9 ? -1 : ans) << '\n';
}

signed main() {
    quick;
    // solve();
    int t;
    cin >> t;
    while (t--) {
        solve();
    }
}
#include <bits/stdc++.h>

#define rep(i, n) for (int i = 0; i < (n); i++)
using namespace std;
typedef long long ll;
const int INF = 1e9;

int main() {
  ll n, ans = 0, tot = 0;
  string s;
  cin >> s;
  int cnt[10] = {};
  int mod[3] = {};
  rep(i, s.size()) {
    int t = s[i] - '0';
    cnt[t]++;
    mod[t % 3]++;
    tot += t;
  }

  if (tot < 3) {
    cout << -1 << endl;
    return 0;
  }

  int m = tot % 3;
  if (m % 3 == 0) {
    cout << 0 << endl;
    return 0;
  } else if (m == 1) {
    if (mod[1] && s.size() > 1) {
      cout << 1 << endl;
      return 0;
    } else if (mod[2] >= 2 && s.size() > 2) {
      cout << 2 << endl;
      return 0;
    } else {
      cout << -1 << endl;
      return 0;
    }
  } else if (m == 2) {
    if (mod[2] && s.size() > 1) {
      cout << 1 << endl;
      return 0;
    } else if (mod[1] >= 2 && s.size() > 2) {
      cout << 2 << endl;
      return 0;
    } else {
      cout << -1 << endl;
      return 0;
    }
  }

  // cout << ans << endl;
  return 0;
}