#include<bits/stdc++.h> using namespace std; int t,i; string a; bool open(char a) { return (a=='(' or a=='[' or a=='{'); } char close(char a) { if(a==')') return '('; if(a==']') return '['; if(a=='}') return '{'; } bool ans; int main() { cin>>t; while(t--) { cin>>a; stack <char> s; ans=true; for(i=0;i<a.size();i++) { if(a[i]=='|') { if(s.empty() or s.top()!='|') s.push('|'); else s.pop(); } else { if(open(a[i])) s.push(a[i]); else if(!s.empty() and s.top()==close(a[i])) s.pop(); else { ans=false; break; } } } if(ans and s.empty()) cout<<"YES"; else cout<<"NO"; cout<<"\n"; } }