#include //#include #include #include #include using namespace std ; #define maxch 4001 void ReadExp ( char * exp ) { char ch ; int i = 0 ; cin.ignore() ; while ( (ch = getchar() ) && ch!= '\n' ) { exp[ i ++ ] = ch ; } exp[ i ] = '\0' ; } void PrintExp ( char * exp ) { int i = 0 ; while( putchar (exp [ i ++ ] ) ) ; putchar('\n') ; } bool Paranthesis( const char * exp ) { int i , t ; int nr = strlen( exp ) ; char * a = (char *)calloc(nr,sizeof(char) ) ; for (t = i = 0 ; i < nr ; ++ i ) { if ( exp[i] == ')' ) { if ( t == 0 || a[ t ]!= '(' ) return 0 ; -- t ; } else if ( exp[i] == ']' ) { if ( t == 0 || a[ t ]!= '[' ) return 0 ; -- t ; } else if ( exp[i] == '}' ) { if ( t == 0 || a[ t ]!= '{' ) return 0 ; -- t ; } else a[ ++t ] = exp[ i ] ; } free ( a ) ; if ( t > 0 ) return 0 ; return 1 ; } int main( ) { //freopen("input.in" , "r", stdin) ; char exp[ maxch ] ; int quest ; scanf("%i" , &quest ) ; while ( quest -- ) { ReadExp( exp ) ; if ( Paranthesis( exp ) ) printf("YES\n") ; else printf("NO\n") ; } return 0 ; }