#include <bits/stdc++.h>
using namespace std;
using ll = long long;

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

  ll T; cin >> T;
  for (ll i = 0; i < T; i++) {
    string S; cin >> S;
    if (S > "atcoder") {
      cout << 0 << '\n';
      continue;
    }
    ll ans = -1;
    for (ll j = 0; j < S.size(); j++) {
      if (S[j] != 'a') {
        ans = j;
        break;
      }
    }
    if (ans != -1) {
      if (S[ans] > 't') {
        ans--;
      }
    }
    cout << ans << '\n';
  }
  return 0;
}
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
typedef long long ll;
typedef pair<ll, ll> P;
typedef vector<string> vs;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef vector<P> vp;
typedef vector<vi> vvi;
typedef vector<vl> vvl;
typedef vector<vp> vvp;

// input
int N;
vi p;


void input() {
    cin >> N;
    p = vi(N);
    rep(i, N) cin >> p[i];
}


int main() {
    input();
    vi ans;
    set<int> s;
    rep(i, N - 1) s.insert(i + 1);
    set<int> mx{p[0]};
    int i = 1;
    while (i < N) {
        if (i == 0) {
            mx.insert(p[0]);
            i++;
            continue;
        }
        if (p[i - 1] < p[i]) {
            mx.insert(p[i]);
            i++;
            continue;
        }
        mx.erase(p[i - 1]);
        mx.insert(p[i]);
        if (*mx.end() == i) {
            if (s.count(i)) {
                s.erase(i);
                ans.push_back(i);
                mx.erase(p[i - 1]);
                swap(p[i - 1], p[i]);
                i--;
                continue;
            } else {
                cout << -1 << endl;
                return 0;
            }
        }
        i++;
    }
    rep(i, N - 1) {
        if (p[i] > p[i + 1]) {
            cout << -1 << endl;
            return 0;
        }
    }
    if (ans.size() != N - 1) {
        cout << -1 << endl;
        return 0;
    }
    for (int x : ans) cout << x << endl;
}
