#include "bits/stdc++.h"

using namespace std;

using ll = long long;
using pii = pair <int, int>;
using pll = pair <ll, ll>;

#define FIO() ios_base::sync_with_stdio(0);cin.tie(NULL);

const ll inf = 9e18;

int main() {
    FIO();

    string atc = "atcoder";

    int tc; cin >> tc;
    while (tc--) {
        string s; cin >> s;
        ll ans = inf;
        ll temp = 0;

        int m = min(atc.size(), s.size());
        int n = s.size();

        bool ok = true;
        for (int i = 0; i < m; i++) {
            if (s[i] > atc[i]) {
                ans = min(ans, temp);
                break;
            }

            for (int j = i + 1; j < n; j++) {
                if (s[j] > atc[i]) {
                    ans = min(ans, temp + j - i);
                    break;
                }
            }

            bool fnd = false;
            for (int j = i; j < n; j++) {
                if (s[j] != atc[i]) continue;

                fnd = true;
                while (s[i] != atc[i]) {
                    swap(s[j], s[j - 1]);
                    j--;
                    temp++;
                }

                break;
            }

            if (!fnd) {
                ok = false;
                break;
            }
        }

        if (ok and m < s.size()) ans = min(ans, temp);

        if (ans == inf) ans = -1;
        cout << ans << '\n';
    }
}

#include <iostream>
#include <cstdio>
#include <string>
#include <cstring>
#include <deque>
#include <list>
#include <queue>
#include <stack>
#include <vector>
#include <utility>
#include <algorithm>
#include <map>
#include <set>
#include <complex>
#include <cmath>
#include <limits>
#include <climits>
#include <ctime>
#include <cassert>
#include <numeric>
#include <functional>
#include <bitset>

using namespace std;
using lint = long long int;
long long int INF = 1001001001001001LL;
int inf = 1000000007;
long long int MOD = 1000000007LL;
double PI = 3.1415926535897932;

template<typename T1,typename T2>inline void chmin(T1 &a,const T2 &b){if(a>b) a=b;}
template<typename T1,typename T2>inline void chmax(T1 &a,const T2 &b){if(a<b) a=b;}

#define ALL(a) a.begin(),a.end()
#define RALL(a) a.rbegin(),a.rend()

int main() {
  
  vector<int> cnt(10, 0);
  string s; cin >> s;
  for (auto c : s) {
    cnt[c - '0']++;
  }

  if (s.size() <= 5) {
    sort(ALL(s));
    do {
      int num = stoi(s);
      if (num % 8 == 0) {
        cout << "Yes" << endl;
        return 0;
      }
    } while (next_permutation(ALL(s)));

    cout << "No" << endl;
    return 0;
  }

  for (int x = 104; x <= 999; x += 8) {
    vector<int> nums(10, 0);
    // cerr << "x = " << x << endl;
    int y = x;
    while (y) {
      // cerr << y % 10 << endl;
      nums[y % 10]++;
      y /= 10;
    }

    bool ok = true;
    for (int i = 0; i <= 9; i++) {
      if (cnt[i] < nums[i]) ok = false;
    }
    if (ok) {
      cerr << "x = " << x << endl;
      cout << "Yes" << endl;
      return 0;
    }
  }

  cout << "No" << endl;
  return 0;
}
