#include //#include #include #include //std::ifstream fin ("paranteze.in" ) ; //std::ofstream fout ("paranteze.out") ; int main() { int i ; int quest ; std::cin >> quest ; std::cin.ignore() ; while ( quest -- ) { std::stack < char > st ; std::string exp ; std::cin >> exp ; int n = exp.size() ; for ( i = 0 ; i < n ; ++ i ) { if ( exp[ i ] == ')' ) { if ( !st.empty() && st.top() == '(' ) st.pop() ; else break ; } else if ( exp[ i ] == ']') { if ( !st.empty() && st.top() == '[' ) st.pop() ; else break ; } else if ( exp[ i ] == '}') { if ( !st.empty() && st.top() == '{' ) st.pop() ; else break ; } else if ( exp[ i ] == '|' && !st.empty() && st.top() == '|') { st.pop() ; } else st.push ( exp [ i ] ) ; } if ( st.empty() && i >= n ) printf("YES\n") ; else printf("NO\n") ; } // fin.close() ; //fout.close() ; return 0 ; }