#include using namespace std; set open; stack st; map pairs; bool ok(char o, char c) { return pairs[o] == c; } bool solve(string &S) { while (!st.empty()) { st.pop(); } int n = (int) S.size(); for (int i = 0; i < n; i++) { char c = S[i]; if (c == '|') { if (st.empty() || st.top() != '|') { st.push(c); } else { st.pop(); } } else { if (open.count(c) > 0) { st.push(c); } else { if (st.empty()) return false; char t = st.top(); st.pop(); if (!ok(t, c)) { return false; } } } } if (!st.empty()) { return false; } return true; } int main() { // assert(freopen("pipes.in", "r", stdin)); // assert(freopen("pipes.out", "w", stdout)); cin.sync_with_stdio(false); int N; string S; open.insert('('); open.insert('{'); open.insert('['); open.insert('|'); pairs['('] = ')'; pairs['{'] = '}'; pairs['['] = ']'; pairs['|'] = '|'; cin >> N; for (int i = 0; i < N; i++) { cin >> S; bool ans = solve(S); if (ans) { cout << "YES\n"; } else { cout << "NO\n"; } } return 0; }