public static void main ( String [ ] args ) {
  Scanner sc = new Scanner ( System . in ) ;
  int t = Integer . parseInt ( sc . nextLine ( ) ) ;
  for ( ;
  t > 0 ;
  t -- ) {
    String a = sc . nextLine ( ) ;
    int n = a . length ( ) ;
    if ( a . charAt ( 0 ) == a . charAt ( n - 1 ) ) {
      System . out . println ( "NO" ) ;
      continue ;
    }
    int [ ] countChars = new int [ 3 ] ;
    for ( int i = 0 ;
    i < n ;
    i ++ ) {
      char letra = a . charAt ( i ) ;
      countChars [ letra - 65 ] ++ ;
    }
    char [ ] equivalencias = new char [ 3 ] ;
    equivalencias [ a . charAt ( 0 ) - 65 ] = '(' ;
    equivalencias [ a . charAt ( n - 1 ) - 65 ] = ')' ;
    for ( int i = 0 ;
    i < 3 ;
    i ++ ) {
      if ( equivalencias [ i ] == 0 ) {
        if ( countChars [ a . charAt ( 0 ) - 65 ] > countChars [ a . charAt ( n - 1 ) - 65 ] ) equivalencias [ i ] = ')' ;
        else equivalencias [ i ] = '(' ;
      }
    }
    int contador = 0 ;
    for ( int i = 0 ;
    i < n ;
    i ++ ) {
      char letra = a . charAt ( i ) ;
      if ( equivalencias [ letra - 65 ] == '(' ) contador ++ ;
      else -- contador ;
      if ( contador < 0 ) break ;
    }
    System . out . println ( ( contador == 0 ? "YES" : "NO" ) ) ;
  }
}

public static void main ( String [ ] args ) { Scanner sc = new Scanner ( System . in ) ; int t = Integer . parseInt ( sc . nextLine ( ) ) ; for ( ; t > 0 ; t -- ) { String a = sc . nextLine ( ) ; int n = a . length ( ) ; if ( a . charAt ( 0 ) == a . charAt ( n - 1 ) ) { System . out . println ( " NO " ) ; continue ; } int [ ] countChars = new int [ 3 ] ; for ( int i = 0 ; i < n ; i ++ ) { char letra = a . charAt ( i ) ; countChars [ letra - 65 ] ++ ; } char [ ] equivalencias = new char [ 3 ] ; equivalencias [ a . charAt ( 0 ) - 65 ] = ' ( ' ; equivalencias [ a . charAt ( n - 1 ) - 65 ] = ' ) ' ; for ( int i = 0 ; i < 3 ; i ++ ) { if ( equivalencias [ i ] == 0 ) { if ( countChars [ a . charAt ( 0 ) - 65 ] > countChars [ a . charAt ( n - 1 ) - 65 ] ) equivalencias [ i ] = ' ) ' ; else equivalencias [ i ] = ' ( ' ; } } int contador = 0 ; for ( int i = 0 ; i < n ; i ++ ) { char letra = a . charAt ( i ) ; if ( equivalencias [ letra - 65 ] == ' ( ' ) contador ++ ; else -- contador ; if ( contador < 0 ) break ; } System . out . println ( ( contador == 0 ? " YES " : " NO " ) ) ; } }
(method_declaration (modifiers) type: (void_type) name: (identifier) parameters: (formal_parameters (formal_parameter type: (array_type element: (type_identifier) dimensions: (dimensions)) name: (identifier))) body: (block (local_variable_declaration type: (type_identifier) declarator: (variable_declarator name: (identifier) value: (object_creation_expression type: (type_identifier) arguments: (argument_list (field_access object: (identifier) field: (identifier)))))) (local_variable_declaration type: (integral_type) declarator: (variable_declarator name: (identifier) value: (method_invocation object: (identifier) name: (identifier) arguments: (argument_list (method_invocation object: (identifier) name: (identifier) arguments: (argument_list)))))) (for_statement condition: (binary_expression left: (identifier) right: (decimal_integer_literal)) update: (update_expression (identifier)) body: (block (local_variable_declaration type: (type_identifier) declarator: (variable_declarator name: (identifier) value: (method_invocation object: (identifier) name: (identifier) arguments: (argument_list)))) (local_variable_declaration type: (integral_type) declarator: (variable_declarator name: (identifier) value: (method_invocation object: (identifier) name: (identifier) arguments: (argument_list)))) (if_statement condition: (parenthesized_expression (binary_expression left: (method_invocation object: (identifier) name: (identifier) arguments: (argument_list (decimal_integer_literal))) right: (method_invocation object: (identifier) name: (identifier) arguments: (argument_list (binary_expression left: (identifier) right: (decimal_integer_literal)))))) consequence: (block (expression_statement (method_invocation object: (field_access object: (identifier) field: (identifier)) name: (identifier) arguments: (argument_list (string_literal)))) (continue_statement))) (local_variable_declaration type: (array_type element: (integral_type) dimensions: (dimensions)) declarator: (variable_declarator name: (identifier) value: (array_creation_expression type: (integral_type) dimensions: (dimensions_expr (decimal_integer_literal))))) (for_statement init: (local_variable_declaration type: (integral_type) declarator: (variable_declarator name: (identifier) value: (decimal_integer_literal))) condition: (binary_expression left: (identifier) right: (identifier)) update: (update_expression (identifier)) body: (block (local_variable_declaration type: (integral_type) declarator: (variable_declarator name: (identifier) value: (method_invocation object: (identifier) name: (identifier) arguments: (argument_list (identifier))))) (expression_statement (update_expression (array_access array: (identifier) index: (binary_expression left: (identifier) right: (decimal_integer_literal))))))) (local_variable_declaration type: (array_type element: (integral_type) dimensions: (dimensions)) declarator: (variable_declarator name: (identifier) value: (array_creation_expression type: (integral_type) dimensions: (dimensions_expr (decimal_integer_literal))))) (expression_statement (assignment_expression left: (array_access array: (identifier) index: (binary_expression left: (method_invocation object: (identifier) name: (identifier) arguments: (argument_list (decimal_integer_literal))) right: (decimal_integer_literal))) right: (character_literal))) (expression_statement (assignment_expression left: (array_access array: (identifier) index: (binary_expression left: (method_invocation object: (identifier) name: (identifier) arguments: (argument_list (binary_expression left: (identifier) right: (decimal_integer_literal)))) right: (decimal_integer_literal))) right: (character_literal))) (for_statement init: (local_variable_declaration type: (integral_type) declarator: (variable_declarator name: (identifier) value: (decimal_integer_literal))) condition: (binary_expression left: (identifier) right: (decimal_integer_literal)) update: (update_expression (identifier)) body: (block (if_statement condition: (parenthesized_expression (binary_expression left: (array_access array: (identifier) index: (identifier)) right: (decimal_integer_literal))) consequence: (block (if_statement condition: (parenthesized_expression (binary_expression left: (array_access array: (identifier) index: (binary_expression left: (method_invocation object: (identifier) name: (identifier) arguments: (argument_list (decimal_integer_literal))) right: (decimal_integer_literal))) right: (array_access array: (identifier) index: (binary_expression left: (method_invocation object: (identifier) name: (identifier) arguments: (argument_list (binary_expression left: (identifier) right: (decimal_integer_literal)))) right: (decimal_integer_literal))))) consequence: (expression_statement (assignment_expression left: (array_access array: (identifier) index: (identifier)) right: (character_literal))) alternative: (expression_statement (assignment_expression left: (array_access array: (identifier) index: (identifier)) right: (character_literal)))))))) (local_variable_declaration type: (integral_type) declarator: (variable_declarator name: (identifier) value: (decimal_integer_literal))) (for_statement init: (local_variable_declaration type: (integral_type) declarator: (variable_declarator name: (identifier) value: (decimal_integer_literal))) condition: (binary_expression left: (identifier) right: (identifier)) update: (update_expression (identifier)) body: (block (local_variable_declaration type: (integral_type) declarator: (variable_declarator name: (identifier) value: (method_invocation object: (identifier) name: (identifier) arguments: (argument_list (identifier))))) (if_statement condition: (parenthesized_expression (binary_expression left: (array_access array: (identifier) index: (binary_expression left: (identifier) right: (decimal_integer_literal))) right: (character_literal))) consequence: (expression_statement (update_expression (identifier))) alternative: (expression_statement (update_expression (identifier)))) (if_statement condition: (parenthesized_expression (binary_expression left: (identifier) right: (decimal_integer_literal))) consequence: (break_statement)))) (expression_statement (method_invocation object: (field_access object: (identifier) field: (identifier)) name: (identifier) arguments: (argument_list (parenthesized_expression (ternary_expression condition: (binary_expression left: (identifier) right: (decimal_integer_literal)) consequence: (string_literal) alternative: (string_literal))))))))))