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

typedef long long ll;

#define pb push_back
#define mp make_pair
#define all(x) (x).begin() ,(x).end()


const int N = 3e5 + 5;
const ll inf = 1e16;

int dx[] = {1 , 0 , -1 , -1 , 0 , 1};
int dy[] = {1 , 1 ,  0 , -1 , -1 , 0};

void solve() {
	string s;
	cin >> s;

	if (s > "atcoder") {
		cout << "0\n";
		return;
	}
	int n = s.size();
	int pos = -1;
	for (int i = 0 ; i < n ; i++) {
		if (s[i] != 'a') {
			pos = i;
			break;
		}
	}

	if (pos == -1)
		cout << "-1\n";
	else if (s[pos] > 't') cout << pos - 1 << "\n";
	else cout << pos << "\n";
}
int main() {

	ios_base::sync_with_stdio(false);
	cin.tie(NULL);
	cout.tie(NULL);

	int t = 1;
	cin >> t;

	while (t--)
		solve();

	return 0;
}
#include <bits/stdc++.h>

#define fi first
#define se second
#define sz(a) (int)(a).size()
#define all(a) (a).begin(), (a).end()
#define reset(a,v) memset((a), v, sizeof(a))

using namespace std;

typedef long long ll;
typedef pair<int,int> ii;
typedef vector<int> vi;
typedef vector<ll> vll;
typedef vector<ii> vii;

const int N = (1<<16) + 5;

int n;
int dat[N];
vi players;

int main() {
	scanf("%d", &n);
	for (int i = 0; i < (1<<n); ++i) {
		scanf("%d", &dat[i]);
		players.push_back(i);
	}

	while (sz(players) > 2) {
		vi newPlayers;
		for (int i = 0; i < sz(players); i += 2) {
			if (dat[players[i]] > dat[players[i+1]]) newPlayers.push_back(players[i]);
			else newPlayers.push_back(players[i+1]);
		}

		players = newPlayers;
	}

	if (dat[players[0]] > dat[players[1]]) printf("%d\n", players[1]+1);
	else printf("%d\n", players[0]+1);
	return 0;
}