#include <iostream>
#include <iostream>
#include <vector>
#include <algorithm>
#include <iomanip>
#include <tuple>
#include <math.h>
#include <set>
#include <cassert>
#include <stack>
#include <bitset>
#include <map>
#include <queue>
#include <random>
#include <unordered_set>
#include <unordered_map>

#define DEBUG
#define fi first
#define se second
#define pqueue priority_queue
#define pb(x) push_back(x)
#define endl '\n'
#define all(x) x.begin(), x.end()
#define int long  long
#define mk(a, b) make_pair(a, b)

using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
typedef vector<int> vi;
typedef vector<vector<int> > vvi;
typedef vector<ull> vull;
typedef vector<ll> vll;
// typedef tuple<ll, ll, ll> tiii;
typedef pair<int, int> pii;
typedef vector<pair<int, int> > vpii;
typedef vector<bool> vb;
typedef vector<string> vs;
typedef vector<vector<ll> > vvll;
typedef vector<char> vc;

const int inf = 1e9 + 228;
const ll infll = 1e18;
const ll MOD = 1e9 + 7;
//static const int maxn = 1e6 + 228;
const ld eps = 1e-5;
const int K = 31;
const ld eps2 = 1e-9;
const ll MOD2 = 998244353;
const ll dosz = 5e5;
const ll SZ = (1 << 18);
const ld PI = 3.1415926535;

void fast_io() {
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    // freopen("a.in", "r", stdin);
    // freopen("262144.out", "w", stdout);
}

void solve() {
    string s;
    cin >> s;
    string temp = "atcoder";
    if(s>temp){
        cout << 0 << endl;
        return;
    }
    int n = s.size();
    for(int i=1; i<n; i++){
        if(s[i] > 't'){
            cout << max(i-1, 0ll) << endl;
            return;
        } if(s[i] > 'a'){
            cout << i << endl;
            return;
        }
    }
    cout << -1 << endl;
}


signed main() {
    fast_io();
    srand(time(NULL));
    // cout << fixed << setprecision(3);
    int q = 1;
    cin >> q;
    while (q--)
        solve();
}
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); i++)
#define rep2(i, a, b) for (int i = (a); i < (b); ++i)
#define all(a) (a).begin(), (a).end()

using namespace std;
using ll = long long;
using P = pair<int, int>;

const ll INF_L = 1LL << 60;
const int INF_I = 1 << 30;
const int MOD = (int)1e9 + 7;
const double PI = acos(-1);
int dx[] = {1, 0, -1, 0};
int dy[] = {0, -1, 0, 1};

int main()
{
    int n;
    cin >> n;
    vector<int> a(n), p(n), x(n);
    rep(i, n) cin >> a[i] >> p[i] >> x[i];

    rep(i, n)
    {
        x[i] -= a[i];
        x[i] = max(x[i], 0);
    }

    int ans = 1001001001;
    rep(i, n)
    {
        if (x[i])
            ans = min(p[i], ans);
    }
    cout << (ans == 1001001001 ? -1 : ans) << "\n";
    return 0;
}
