#include <iostream>
//#include <fstream>
#include <stdio.h>
#include <string.h>
#include <malloc.h>

using namespace std ;

#define maxch 4001


void ReadExp ( char * exp )
{
    char ch ;
    int i  = 0 ;

    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 if (exp[ i ]=='|' && t  && a[ t ] == '|') -- 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 ] ;
    short quest ;
    scanf("%i" , &quest ) ;

    cin.ignore() ;
    while ( quest -- )
    {
        ReadExp( exp )  ;
        //PrintExp( exp ) ;

        if ( Paranthesis( exp ) )  printf("YES\n") ;

        else printf("NO\n") ;

    }




    return  0 ;

}