for s in[*open(0)][1:]:
r=0,
for u in s[:-1]:r+=r[-1]+2*(u==max('ABC',key=s.count))-1,
print('YNEOS'[r[-1]|min(r)*max(r)!=0::2])
using namespace std;
#include<bits/stdc++.h>
#include <iostream>
#include <cmath>
#define _DrIndex_ ios_base::sync_with_stdio(0);cin.tie(0),cout.tie(0);
#define clr(x,val) memset(x,val,sizeof(x))
#define read(FILE) freopen(FILE, "r", stdin);
#define write(FILE) freopen(FILE, "w", stdout);
#define ll long long
#define ul unsigned int
#define ull unsigned long long
#define ld long double
/*===================================================*/
bool preceed(pair<char,int> x , pair<char,int> y) {return x > y;}
/*===================================================*/
bool sortbyCond(const pair<int, int>& a,
const pair<int, int>& b)
{
if (a.first != b.first)
return (a.first < b.first);
else
return (a.second > b.second);
}
/*===================================================*/
int factDigits(int n){
if(n<0)return 0;if(n<=1)return 1;double digits=0;
for (int i = 2; i <= n; ++i) {
digits+= log10(i);
}
return floor(digits)+1;
}
/*===================================================*/
void primeFactors(int n){int c=2;while(n>1){if(n%c==0){std::cout<<c<<" ";n/=c;}else c++;}}
/*===================================================*/
bool cmp(std::pair<std::string,int>&a,std::pair<std::string,int>&b){return a.second>b.second;}
/*===================================================*/
bool absPreceed(int x , int y){return abs(x)< abs(y);}
/*===================================================*/
ll noOfDigits(ll n){return 1+floor(log10(n));}
/*===================================================*/
ll gcd(ll a, ll b)
{
if (a == 0) return b;
return gcd(b%a, a);
}
/*===================================================*/
ll lcm(ll a, ll b)
{
return (a / gcd(a, b))*b;
}
/*===================================================*/
// kadane's algorithm for getting max sum of sub array in O(n)
int maxSubArraySum(int array[], int size)
{
int max_so_far = INT_MIN, max_ending_here = 0;
for (int i = 0; i < size; i++)
{
max_ending_here = max_ending_here + array[i];
if (max_so_far < max_ending_here)
max_so_far = max_ending_here;
if (max_ending_here < 0)
max_ending_here = 0;
}
return max_so_far;
}
/*===================================================*/
long long MOD = 1e9 + 7;
ll fast_power(ll base , ll power){
ll res=1;
while (power > 0){
if(power & 1){
res=(res*base)%MOD;
}
base = (base*base) % MOD;
power >>= 1;
}
return res;
}
/*===================================================*/
// get no. ones in the binary representation
int popCount(int num){
int res=0;
while(num > 0){
res += num & 1;
num >>= 1;
}return res;
}
/*===================================================*/
void read_Write() {
ios::sync_with_stdio(false),
cin.tie(nullptr),
cout.tie(nullptr);
#ifndef ONLINE_JUDGE
read("input.txt")
write("output.txt")
#else
# endif
}
const int N = 1e6+5;
int main() {
_DrIndex_
//read_Write();
int tt=1;cin>>tt;
while (tt--) {
string s;cin >> s;char a[3]{'A' , 'B' , 'C'};bool ff=false;
for(int mask = 1 ; mask < (1 << 3) ; mask++){
string res;
for(int i = 0 ; i < s.size() ; i++){
int ind = distance(a,find(a,a+3,s[i]));
if(mask & (1 << ind))res+='(';
else res+=')';
}
stack<char> st;bool f=false;
for (int i = 0; i < res.size(); i++) {
if (res[i] == '(') st.push(s[i]);
else {
if (st.empty()) {f =true;break;}
else st.pop();
}
}if(st.empty() && f == false){ff=true;break;}
}cout << (ff ? "YES" : "NO") << '\n';
}
return 0;
}
1144A - Diverse Strings | 1553B - Reverse String |
1073A - Diverse Substring | 630N - Forecast |
312B - Archer | 34D - Road Map |
630I - Parking Lot | 160B - Unlucky Ticket |
371B - Fox Dividing Cheese | 584B - Kolya and Tanya |
137B - Permutation | 550C - Divisibility by Eight |
5A - Chat Servers Outgoing Traffic | 615A - Bulbs |
5B - Center Alignment | 549A - Face Detection |
535B - Tavas and SaDDas | 722C - Destroying Array |
366A - Dima and Guards | 716B - Complete the Word |
1461C - Random Events | 1627A - Not Shading |
141B - Hopscotch | 47B - Coins |
1466C - Canine poetry | 74A - Room Leader |
1333D - Challenges in school №41 | 1475B - New Year's Number |
461A - Appleman and Toastman | 320B - Ping-Pong (Easy Version) |