#define _GLIBCXX_DEBUG
#include <bits/stdc++.h>
#include <iostream>

using namespace std;
using uint = unsigned int;
using ll = long long;
using ull = unsigned long long;

#define FOR(i,a,b) for(int i=(a);i<(b);++i)
#define REP(i,n)  FOR(i,0,n)

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

    int T;
    cin >> T;
    int ans[100];
    REP(i, T) {
        string S;
        cin >> S;
        ans[i] = -1;
        if ("atcoder" < S)
            ans[i] = 0;
        else{
            REP(j, S.length())
            {
                if(S[j] > 'a') {
                    if(S[j] > 't')
                        ans[i] = j-1;
                    else
                        ans[i] = j;
                    break;
                }
            }
        }

    }
    REP(i,T)
    cout << ans[i] << "\n";
}
#include <bits/stdc++.h>
#define REP(i, n) for (int i = 0; (i) < (int)(n); ++(i))
#define REP3(i, m, n) for (int i = (m); (i) < (int)(n); ++(i))
#define REP_R(i, n) for (int i = (int)(n)-1; (i) >= 0; --(i))
#define REP3R(i, m, n) for (int i = (int)(n)-1; (i) >= (int)(m); --(i))
#define ALL(x) ::std::begin(x), ::std::end(x)
#define SZ(x) ((int)(x).size())
#define YESNO(bool)            \
    if (bool) {                \
        cout << "YES" << endl; \
    } else {                   \
        cout << "NO" << endl;  \
    }
#define yesno(bool)            \
    if (bool) {                \
        cout << "yes" << endl; \
    } else {                   \
        cout << "no" << endl;  \
    }
#define YesNo(bool)            \
    if (bool) {                \
        cout << "Yes" << endl; \
    } else {                   \
        cout << "No" << endl;  \
    }
#define endl "\n"
#define pb push_back
#define mp make_pair
#define ft first
#define sd second
using namespace std;
using ll = long long;
using ull = unsigned long long;
using vi = vector<int>;
using vll = vector<long long>;
using vvi = vector<vi>;
using vvll = vector<vll>;
using vs = vector<string>;
using pii = pair<int, int>;
template <class T>
bool chmax(T &a, const T &b) {
    if (a < b) {
        a = b;
        return true;
    } else
        return false;
}
template <class T>
bool chmin(T &a, const T &b) {
    if (b < a) {
        a = b;
        return true;
    } else
        return false;
}
template <typename T>
inline void print(const T &x) {
    cout << x << "\n";
}
template <typename T>
inline void print(const vector<T> &v, string s = " ") {
    REP (i, v.size())
        cout << v[i] << (i != (ll)v.size() - 1 ? s : "\n");
}
const int IINF = 0x3f3f3f3f;
const long long LINF = 0x3f3f3f3f3f3f3f3fLL;

string solve(string S) {
    // TODO: edit here
    string ret = "";
    for (ll i = SZ(S) - 1; i >= 0; i--) {
        if (S[i] == '0') ret += "0";
        else if (S[i] == '1') ret += "1";
        else if (S[i] == '6') ret += "9";
        else if (S[i] == '8') ret += "8";
        else if (S[i] == '9') ret += "6";
    }
    return ret;
}

// generated by oj-template v4.7.2 (https://github.com/online-judge-tools/template-generator)
int main() {
    std::ios::sync_with_stdio(false);
    std::cin.tie(nullptr);
    string S;
    cin >> S;
    auto ans = solve(S);
    cout << ans << endl;
    return 0;
}
