#include using namespace std; #define mp make_pair #define pb push_back #define ll long long string s; vector open, cl; int n; bool isOpen(char c) { if (c == '(' || c == '[' || c == '{') { return true; } return false; } bool check(int x, int y) { for (int i = 0; i < open.size(); ++i) { if (open[i] == s[x]) { if (s[y] != cl[ i ]) { //cout << " " << i << " " << s[y] << " " << cl[i] << "\n"; return false; } } } return true; } int main() { ios::sync_with_stdio(false); #ifndef ONLINE_JUDGE freopen("test.in", "r", stdin); //freopen("test.out", "w", stdout); #endif cin >> n; cin.get(); open.pb('('); open.pb('['); open.pb('{'); //open.pb('|'); cl.pb(')'); cl.pb(']'); cl.pb('}'); //cl.pb('|'); for (int i = 1; i <= n; ++i) { cin >> s; stack st; if (s.size() == 0) { cout << "YES\n" << "\n"; continue; } bool flag = true; for (int j = 0; j < s.size(); ++j) { if (s[j] == '|') { if (st.empty() || s[st.top()] != '|') { st.push(j); } else { st.pop(); } continue; } if (isOpen(s[j])) { st.push(j); } else { if (st.empty()) { flag = false; break; } if (check(st.top(), j)) { st.pop(); } else { //cout << st.top() << " " << j << "\n"; flag = false; break; } } } if (!st.empty()) { flag = false; } if (flag) { cout << "YES\n"; } else { cout << "NO\n"; } } return 0; }