// AUTHOR:-  Harsh Thete
//   IIIT-Allahabad
#include <bits/stdc++.h>
using namespace std;

#define ll long long

#define fl(i,a,b) for(ll i=a;i<b;i++)

#define fst ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
#define TT 	int TT;cin>>TT;while(TT--)

#define pb push_back
#define ub upper_bound
#define lb lower_bound
#define mp make_pair
#define sz size

void my_ans_is()
{
    string s;cin>>s;//ll ct=0;
    ll n=s.length();
    vector<ll>v(27);ll ct=0;
    if(s[0]==s[n-1]) {cout<<"NO\n";return;}
    v[s[0]-'A']=1;v[s[n-1]-'A']=-1;
    fl(i,1,n-1) if(s[i]!=s[0]&&s[i]!=s[n-1]) ct++;
    if(ct>0)
    {
       fl(i,1,n-1) if(s[i]!=s[n-1]&&s[i]!=s[0]) v[s[i]-'A']=1; //AABBAC
       ct=1;
        fl(i,1,n)
        {ct+=v[s[i]-'A'];if(ct<0) break;}
        if(ct==0) cout<<"YES\n";
        else
        {
            ct=1;
            
       fl(i,1,n-1) if(s[i]!=s[n-1]&&s[i]!=s[0]) v[s[i]-'A']=-1;
       
        fl(i,1,n)
        {ct+=v[s[i]-'A'];if(ct<0) break;}
        if(ct==0) cout<<"YES\n";
        else cout<<"NO\n";
        }
    }
    else
    {
        ct=1;
        fl(i,1,n)
        {ct+=v[s[i]-'A'];if(ct<0) break;}
        if(ct!=0) cout<<"NO\n";
        else cout<<"YES\n";
    }
}//cin.getline(s,1000) to get string cointaining spaces where s->char s[1000] to find exact len n=strlen(s);
int main() 
{         // Euoiler tot phi(n)=cnt of numbers less than n (from 1to n) whose gcd with n is 1
	     //  for tot cnt of gcd=1 from x to n-> cnt= phi(n/gcd(x,n))
	                                //////////////__gcd(x,y)=__gcd(x-y,y)
	

        fst;
        TT
	{      
         my_ans_is();
// Note:-Check bounds in arrays, check limit of int (signed integer overflow)
        }
   return 0;
}