#include <iostream>
#include <vector>
using namespace std;

#define FOR(i, a, b) for (int i = a; i < b; ++i)
#define REP(i, n) for (int i = 0; i < n; ++i)
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;

int main() {
  int n;
  cin >> n;
  vector<int> cnt(2000001, 0);
  int pi;
  int x = 0;
  REP(i, n) {
    cin >> pi;
    cnt[pi]++;
    while (cnt[x] > 0) {
      x++;
    }
    cout << x << endl;
  }
}


#include <bits/stdc++.h>

using namespace std;
typedef long long ll;

void solve() {
    string t; cin >> t;
    int n = t.size();
    string s = "atcoder", p = t;
    sort(p.rbegin(), p.rend());
    if (p <= s) {
        cout << -1 << endl;
        return;
    }
    if (t > s) {
        cout << 0 << endl;
        return;
    }
    int m = n;
    for (int i = n - 1; i >= 0; i--) {
        for (int j = min(6, i - 1); j >= 0; j--) {
            swap(t[i], t[j]);
            if (s < t) {
                m = min(m, i - j);
            }
            swap(t[i], t[j]);
        }
    }
    cout << m << endl;
}

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    int t; cin >> t;
    while (t--) {
        solve();
    }
    return 0;
}
