#include #include #include #include #include #include #include #include #include #include #include #include using namespace std; const int N = 5005; char s[N]; string cc = "{}[]()|"; bool isop(char c) { int id = -1; for(int i = 0; i < cc.size(); ++i) if(cc[i] == c) id = i; return c == '|' || id % 2 == 0; } bool ispair(char c1, char c2) { if(c1 == c2 && c1 == '|') return true; int id1 = -1; for(int i = 0; i < cc.size(); ++i) if(cc[i] == c1) id1 = i; int id2 = -1; for(int i = 0; i < cc.size(); ++i) if(cc[i] == c2) id2 = i; return id2 == id1 + 1; } int main() { int t; cin >> t; while(t--) { scanf("%s", s); int n = strlen(s); stack st; bool ok = 1; for(int i = 0; i < n; ++i) { if(isop(s[i])) { if(s[i] == '|' && !st.empty() && st.top() == s[i]) st.pop(); else st.push(s[i]); } else { if(st.empty()) { ok = 0; break; } char c = st.top(); st.pop(); if(!ispair(c, s[i])) { ok = 0; break; } } } if(!st.empty()) ok = 0; cout << (ok ? "YES" : "NO") << endl; } return 0; }