#include<bits/stdc++.h>
#define ll long long
#define ld long double
#define pb push_back
#define ff first
#define ss second
#define mp make_pair
#define ins insert
#define sb substr
#define con continue
#define ub upper_bound
#define lb lower_bound
#define sp fixed<<setprecision
#define bp __builtin_popcountll
#define pll pair<ll,ll>
#define pld pair<ld,ld>
#define FOR(i,n) for(i=0;i<n;i++)
#define FOR(i,n,a) for(i=a;i<n;i++)
#define FORB(i,n) for(i=n-1;i>=0;i--)
#define FORB(i,n,a) for(i=n-1;i>=a;i--)
#define input(i,n,A) FOR(i,n,0)cin>>A[i]
#define enter cout<<"\n"
#define MOD 1000000007
#define ar array
#define vll vector<ll>
using namespace std;
//ll fact[1000001],power[1000001];
ll powerr(ll x, ll y);
ll mpower(ll x, ll y, ll p);
ll gcdExtended(ll a, ll b);
bool isPrime(ll n);
vll v[100001];
char v1[100001];
ll f = 0;
ll A[100001];
void dividn(ll A1[],ll l,ll r, ll p){
    if(l > r){
        return;
    }
    else{
        ll i;
        ll mx = 0;
        ll z;
        FOR(i,r + 1,l){
            if(A1[i] > mx){
                mx = A1[i];
                z = i;
            }
        }
      //  ll x = p;
        A[z] = p;
        p++;
        //dividn(A,z + 1,r,p);
      //  p = x;
        dividn(A1,l,z - 1,p);
        dividn(A1,z + 1,r,p);
    }
}
bool sortbysec(const pair<int,int> &a, 
              const pair<int,int> &b) 
{ 
    return (a.second < b.second); 
}
int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    ll t,j,k,l,h,p,q,r,x,i,y,z,a,b,c,d,n,m;
    char ch,ch2,ch3;
    string s1,s2,s3;
    //vector<ll> v,w;
    //set<ll> S;
    set<pair<ll,ll> > S1,S;
    t = 1;
    cin>>t;
    x = 0;
   // vll v[100000*5 + 1];
   // queue<pair<ll,ll> > q
//    vector<char *> v;
    ll N = pow(2,32) - 1;
    while(t--){
	    string s;
	    cin >> s;
	    n = s.size();
	    a = 0;
	    b = 0;
	    c = 0;
	    FOR(i,n,0){
		    if(s[i] == 'A')a++;
		    if(s[i] == 'B')b++;
		    if(s[i] == 'C')c++;
	    }
	    if(a - (b + c)==0){
		    ll ans = 0;
		    p = 0,z = 0;
		    FOR(i,n,0){
			    if(s[i] == 'A')ans++;
			    else ans--;
			    if(ans > 0)p++;
			    else if(ans < 0)z++;
		    }
		    if(p && z)cout << "NO\n";
		    else cout << "YES\n";
		    continue;
	    }
	    else if(b - (a + c)==0){
		    ll ans = 0;
		    p = 0,z = 0;
		    FOR(i,n,0){
			    if(s[i] == 'B')ans++;
			    else ans--;
			    if(ans > 0)p++;
			    else if(ans < 0)z++;
		    }
		    if(p && z) cout << "NO\n";
		    else cout << "YES\n";
		    continue;
	    }
	    else if(c - (a + b) == 0){
		    ll ans = 0;
		    p = 0,z = 0;
		    FOR(i,n,0){
			    if(s[i] == 'C')ans++;
			    else ans--;
			    if(ans > 0 )p++;
			    else if(ans < 0)z++;
		    }
		    if(p && z)cout << "NO\n";
		    else cout << "YES\n";
		    continue;
	    }
	    cout << "NO\n";
		
    }
}
ll gcdExtended(ll a, ll b){
    if (b == 0){
        return a;
    }
    else
        return gcdExtended(b,a%b);
}

ll mpower(ll x, ll y, ll p){
    ll res = 1;
    x = x % p;
    while(y > 0){
        if(y & 1) res = (res*x) % p;
        y = y>>1;
        x = (x*x) % p;
    }
    return res;
}

ll powerr(ll x, ll y){
    ll res = 1;
    while (y > 0){
        if (y & 1) res = res*x;
        y = y>>1;
        x = x*x;
    }
    return res;
}

bool isPrime(ll n){
    if (n <= 1)  return false;
    if (n <= 3)  return true;
    if (n%2 == 0 || n%3 == 0) return false;
    ll p=sqrt(n);
    for(int i=5;i<=p;i=i+6)
        if (n%i == 0 || n%(i+2) == 0)
            return false;
    return true;
}

### Prompt

Please formulate a cpp solution to the following problem:

You are given a string a, consisting of n characters, n is even. For each i from 1 to n a_i is one of 'A', 'B' or 'C'.

A bracket sequence is a string containing only characters "(" and ")". A regular bracket sequence is a bracket sequence that can be transformed into a correct arithmetic expression by inserting characters "1" and "+" between the original characters of the sequence. For example, bracket sequences "()()" and "(())" are regular (the resulting expressions are: "(1)+(1)" and "((1+1)+1)"), and ")(", "(" and ")" are not.

You want to find a string b that consists of n characters such that: 

  * b is a regular bracket sequence; 
  * if for some i and j (1 ≤ i, j ≤ n) a_i=a_j, then b_i=b_j. 



In other words, you want to replace all occurrences of 'A' with the same type of bracket, then all occurrences of 'B' with the same type of bracket and all occurrences of 'C' with the same type of bracket.

Your task is to determine if such a string b exists.

Input

The first line contains a single integer t (1 ≤ t ≤ 1000) — the number of testcases.

Then the descriptions of t testcases follow.

The only line of each testcase contains a string a. a consists only of uppercase letters 'A', 'B' and 'C'. Let n be the length of a. It is guaranteed that n is even and 2 ≤ n ≤ 50.

Output

For each testcase print "YES" if there exists such a string b that: 

  * b is a regular bracket sequence; 
  * if for some i and j (1 ≤ i, j ≤ n) a_i=a_j, then b_i=b_j. 



Otherwise, print "NO".

You may print every letter in any case you want (so, for example, the strings yEs, yes, Yes and YES are all recognized as positive answer).

Example

Input


4
AABBAC
CACA
BBBBAC
ABCA


Output


YES
YES
NO
NO

Note

In the first testcase one of the possible strings b is "(())()".

In the second testcase one of the possible strings b is "()()".

### Response

```cpp
#include<bits/stdc++.h>
#define ll long long
#define ld long double
#define pb push_back
#define ff first
#define ss second
#define mp make_pair
#define ins insert
#define sb substr
#define con continue
#define ub upper_bound
#define lb lower_bound
#define sp fixed<<setprecision
#define bp __builtin_popcountll
#define pll pair<ll,ll>
#define pld pair<ld,ld>
#define FOR(i,n) for(i=0;i<n;i++)
#define FOR(i,n,a) for(i=a;i<n;i++)
#define FORB(i,n) for(i=n-1;i>=0;i--)
#define FORB(i,n,a) for(i=n-1;i>=a;i--)
#define input(i,n,A) FOR(i,n,0)cin>>A[i]
#define enter cout<<"\n"
#define MOD 1000000007
#define ar array
#define vll vector<ll>
using namespace std;
//ll fact[1000001],power[1000001];
ll powerr(ll x, ll y);
ll mpower(ll x, ll y, ll p);
ll gcdExtended(ll a, ll b);
bool isPrime(ll n);
vll v[100001];
char v1[100001];
ll f = 0;
ll A[100001];
void dividn(ll A1[],ll l,ll r, ll p){
    if(l > r){
        return;
    }
    else{
        ll i;
        ll mx = 0;
        ll z;
        FOR(i,r + 1,l){
            if(A1[i] > mx){
                mx = A1[i];
                z = i;
            }
        }
      //  ll x = p;
        A[z] = p;
        p++;
        //dividn(A,z + 1,r,p);
      //  p = x;
        dividn(A1,l,z - 1,p);
        dividn(A1,z + 1,r,p);
    }
}
bool sortbysec(const pair<int,int> &a, 
              const pair<int,int> &b) 
{ 
    return (a.second < b.second); 
}
int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    ll t,j,k,l,h,p,q,r,x,i,y,z,a,b,c,d,n,m;
    char ch,ch2,ch3;
    string s1,s2,s3;
    //vector<ll> v,w;
    //set<ll> S;
    set<pair<ll,ll> > S1,S;
    t = 1;
    cin>>t;
    x = 0;
   // vll v[100000*5 + 1];
   // queue<pair<ll,ll> > q
//    vector<char *> v;
    ll N = pow(2,32) - 1;
    while(t--){
	    string s;
	    cin >> s;
	    n = s.size();
	    a = 0;
	    b = 0;
	    c = 0;
	    FOR(i,n,0){
		    if(s[i] == 'A')a++;
		    if(s[i] == 'B')b++;
		    if(s[i] == 'C')c++;
	    }
	    if(a - (b + c)==0){
		    ll ans = 0;
		    p = 0,z = 0;
		    FOR(i,n,0){
			    if(s[i] == 'A')ans++;
			    else ans--;
			    if(ans > 0)p++;
			    else if(ans < 0)z++;
		    }
		    if(p && z)cout << "NO\n";
		    else cout << "YES\n";
		    continue;
	    }
	    else if(b - (a + c)==0){
		    ll ans = 0;
		    p = 0,z = 0;
		    FOR(i,n,0){
			    if(s[i] == 'B')ans++;
			    else ans--;
			    if(ans > 0)p++;
			    else if(ans < 0)z++;
		    }
		    if(p && z) cout << "NO\n";
		    else cout << "YES\n";
		    continue;
	    }
	    else if(c - (a + b) == 0){
		    ll ans = 0;
		    p = 0,z = 0;
		    FOR(i,n,0){
			    if(s[i] == 'C')ans++;
			    else ans--;
			    if(ans > 0 )p++;
			    else if(ans < 0)z++;
		    }
		    if(p && z)cout << "NO\n";
		    else cout << "YES\n";
		    continue;
	    }
	    cout << "NO\n";
		
    }
}
ll gcdExtended(ll a, ll b){
    if (b == 0){
        return a;
    }
    else
        return gcdExtended(b,a%b);
}

ll mpower(ll x, ll y, ll p){
    ll res = 1;
    x = x % p;
    while(y > 0){
        if(y & 1) res = (res*x) % p;
        y = y>>1;
        x = (x*x) % p;
    }
    return res;
}

ll powerr(ll x, ll y){
    ll res = 1;
    while (y > 0){
        if (y & 1) res = res*x;
        y = y>>1;
        x = x*x;
    }
    return res;
}

bool isPrime(ll n){
    if (n <= 1)  return false;
    if (n <= 3)  return true;
    if (n%2 == 0 || n%3 == 0) return false;
    ll p=sqrt(n);
    for(int i=5;i<=p;i=i+6)
        if (n%i == 0 || n%(i+2) == 0)
            return false;
    return true;
}

```