#include<bits/stdc++.h> using namespace std; int main(){ int t; cin>>t; while(t--){ //stack<char> st; map<char,int> mp; map<char,int> ord; string x; cin>>x; int o=0; for(auto i:x){ mp[i]++; ord[i]+=o++; } if(mp['{']==mp['}'] and ord['{']<ord['}'] and mp['(']==mp[')'] and ord['(']<ord[')'] and mp['[']==mp[']'] and ord['[']<ord[']'] and mp['|']%2==0){ puts("YES"); }else{ puts("NO"); } } return 0; }