#include<stdio.h>
#include<string.h>
#define rep(i,N) for(int i=0;i<(int)N;i++)
static inline int Max(size_t a,size_t b){return a>b?(int)a:(int)b;}
static inline int Min(size_t a,size_t b){return a<b?(int)a:(int)b;}
int main()
{
  int T;if(!scanf("%d",&T)){return -1;}
  while(T--)
  {
    char S[1001];int aa=1;
    if(!scanf("%s",S)){return -1;}
    if(strncmp("atcoder",S,Max(strlen(S),7))<0){puts("0");continue;}
    else
    {
      if(S[0]=='a'&&S[1]>'a'){puts("1");continue;}
      rep(i,6)
      {
        if(S[i+1]>'t'){printf("%d\n",i);goto jump;}
        if(S[i+1]>'a'){printf("%d\n",i+1);goto jump;}
        if(i==(int)strlen(S)-1){break;}
      }
      rep(i,(int)strlen(S)-1){if(S[i+1]!='a'){aa=0;break;}}
      if(aa){puts("-1");continue;}
      rep(i,(int)strlen(S)-1)
      {
        if(S[i+1]>'t'){printf("%d\n",i);goto jump;}
        if(S[i+1]>'a'){printf("%d\n",i+1);goto jump;}
      }
      puts("-1");jump:continue;
    }
  }
}
#include<bits/stdc++.h>
using namespace std;
#define ll long long

void solve() {
    int n;
    cin >> n;
    vector<pair<int, pair<int, int>>> v(n);
    for(int i = 0; i < n; ++i) {
        cin >> v[i].second.first >> v[i].first >> v[i].second.second;
    }

    sort(v.begin(), v.end());
    
    for(int i = 0; i < n; ++i) {
        if(v[i].second.first < v[i].second.second) {
            cout << v[i].first;
            return;
        }
    }
    cout << "-1";
}

int main() {
    int t = 1;
    // cin >> t;
    while(t--) {
        solve();
    }
}