#include #include #include using namespace std; char text[4001]; int main(){ //freopen("baruri.in", "r", stdin); int T; scanf("%d", &T); while(T--){ scanf("%s", text); stack S; int len = strlen(text); for(int i = 0; i < len; i++){ char c = text[i]; if(!S.empty()){ char top = S.top(); if(c==')' && top=='(' || c==']' && top=='[' || c=='|' && top=='|' || c=='}' && top=='{'){ S.pop(); }else S.push(c); }else S.push(c); } if(S.empty()){ printf("YES\n"); }else printf("NO\n"); } return 0; }