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

typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
typedef pair<ll,ll> pll;
typedef pair<ll,pll> plp;
typedef pair<pll,ll> ppl;
typedef pair<pll,pll> ppp;
typedef vector<ll> vll;
typedef vector<vll> vvll;
typedef vector<vvll> vvvll;
typedef vector<pll> vpll;
typedef vector<plp> vplp;
typedef vector<ppl> vppl;
typedef vector<ppp> vppp;
typedef vector<vpll> vvpll;
typedef vector<bool> vbool;
typedef vector<vbool> vvbool;
typedef vector<double> vd;
typedef priority_queue<pll,vpll,greater<pll> > pqpll;
typedef priority_queue<pll,vpll> pqrpll;
typedef priority_queue<ll, vll, greater<ll> > pqll;
typedef priority_queue<ll, vll> pqrll;

struct edge{ ll to,cost; edge(ll to,ll cost): to(to), cost(cost){} };
typedef vector<vector<edge>> Graph;

#define rep(i,a,n) for(ll i = a;i < n;i++)
#define rrep(i,a,n) for(ll i = n-1; i >= a;i--)
#define LINF 1000000000000000000
#define INF 1000000000
#define fs first
#define sc second
#define EPS 1e-10
#define ALL(a) a.begin(), a.end()

template<typename T> ll sz(vector<T> &vec){ return (ll)vec.size(); }
template<typename T> ll sz(priority_queue<T, vector<T> > &pq) {return (ll)pq.size(); }
template<typename T> ll sz(priority_queue<T, vector<T>, greater<T> > &pq) {return (ll)pq.size(); }
ll sz(string &s) {return (ll)s.size(); } 

void chmin(ll &a, ll &b) { if(a > b) a = b; }
void chmax(ll &a, ll &b) { if(a < b) a = b; }

ll gcd(ll a,ll b){ return ((!b) ?a :gcd(b, a%b)); }
ll lcm(ll a,ll b){ return a / gcd(a,b) * b; }
ll dx[4] = {0,-1,1,0},dy[4] = {-1,0,0,1};
bool checkindex(ll i,ll n){ return (i < n && i >= 0); }

int main(){
    ll t;
    cin >> t;
    rep(i,0,t){
        string s,t;
        cin >> s;
        string memo = "atcoder";

        t = s;
        sort(ALL(t),greater<char>());
        
        if(memo < s) cout << 0 << endl;
        else if(memo >= t) cout << -1 << endl;
        else{
            ll ans = LINF;
            rep(i,0,sz(memo)){
                rep(j,i,sz(s)){
                    if(s[j] > memo[i]){
                        ans = min(ans,j-i);
                        break;
                    }
                }
                if(s[i] < memo[i]) break;
            }
            cout << ans << endl;
        }
    }

    return 0;
}
/*
                Kids were laughing in my classes
                While I was scheming for the masses
                Who do you think you are?
                Dreaming 'bout being a big star
                They say you're basic, they say you're easy
                You're always riding in the back seat
                Now I'm smiling from the stage while
                You were clapping in the nose bleeds
                Thunder
                Thunder, thun', thunder
                Thun-thun-thunder, thunder, thunder
                Thunder, thun', thunder
                Thun-thun-thunder, thunder
                Thunder, feel the thunder
                Lightning then the thunder
                Thunder, feel the thunder
                Lightning then the thunder
                Thunder
*/
#pragma GCC optimize("Ofast")  
#pragma GCC target("avx,avx2,fma")
#include <bits/stdc++.h>
using namespace std;
#define ll long long 
#define ld long double
#define fi first
#define sec second
#define pb push_back
#define mp(x,y) make_pair(x,y)
#define mod 1000000007
#define me(i,x,n) for( i=x;i<n;i++)
#define she(i,x,n) for( i=x;i>=0;i--)
#define all(v) v.begin(),v.end()
#define pii pair<int,int>
#define pll pair<ll,ll>
#define thunder ios_base::sync_with_stdio(false);cin.tie(NULL);
#define try cin>>t;while(t--)
#define PI (ld)3.141592653589793238
#define INF LLONG_MAX
#define MAX 100005
#define make_graph(k) ll x,y; me(i,0,k){cin>>x>>y; adj[x].pb(y); adj[y].pb(x);}
int main() 
{    
    thunder
      #ifndef ONLINE_JUDGE
      freopen("input.txt", "r", stdin);
      freopen("output.txt", "w", stdout);
      #endif
	ll t,n,m,i,s,j,k,c;
	//try
	
	    s=0;
	    cin>>n;
	    vector <ll> vec(n);
	    me(i,0,n)
	    cin>>vec[i];
        ll p=pow(2,n);
        s=INF;
    for(i=0;i<p;i++)
    {
        ll ans=0;
        ll c=0;
        for(j=0;j<n;j++)
        {
            
            if((i&(1<<j))!=0)
            {
                c=c|vec[j];
                ans=ans^c;
                c=0;
            }
            else
            c=c|vec[j];
        }
        ans=ans^c;
        s=min(s,ans);
    }
    cout<<s<<endl;
   
	return 0;
}


ll gcd(ll a, ll b) 
{ 
    if (b == 0) 
        return a; 
    return gcd(b, a % b);  
} 
ll power(ll x, ll y, ll m) 
{ 
    if (y == 0) 
        return 1; 
    ll p = power(x, y/2, m) % m; 
    p = (p * p) % m; 
  
    return (y%2 == 0)? p : (x * p) % m; 
} 
ll modInverse(ll a, ll m) 
{ 
    //ll g = gcd(a, m); 
    return  power(a, m-2, m); 
} 

//WHY ARE YOU STALKING?

