#include <bits/stdc++.h>
using namespace std;
using ll = long long int;
using ld = long double;
using PII=pair<ll,ll>;
using PIII=pair<int,pair<int,int>>;
const ld ESP = 1e-10;
const ld PI = acosl(-1);
const int N=1e5+10;
const int M=2e5+10;
// const int mod = 1000000007;
const int mod = 998244353;
//随机化
random_device rd;
mt19937 gen(rd());
uniform_int_distribution<> dis(1, 1000000000);
// cout<<fixed<<setprecision(10);
int g(char x){
    if(x=='{'||x=='}')return 4;
    else if(x=='['||x==']')return 3;
    else if(x=='('||x==')')return 2;
    else return 1;
}
bool f(char x){
    return (x=='{'||x=='['||x=='('||x=='<');
}
void solve(){
    string s;
    cin>>s;
    stack<char> t;
    for(int i=0;i<(int)s.size();i++){
        if(f(s[i])){
            if(t.empty()){
                t.push(s[i]);
            }else{
                int d=g(s[i]);
                int c=g(t.top());
                if(d<=c){
                    t.push(s[i]);
                }else{
                    cout<<"NO"<<'\n';
                    return ;
                }
            }
        }else{
            if(t.empty()){
                cout<<"NO"<<'\n';
                return ;
            }
            int d=g(s[i]);
            int c=g(t.top());
            if(d==c){
                t.pop();
            }else{
                cout<<"NO"<<'\n';
                return ;
            }
        }
    }
    cout<<"YES"<<'\n';
}

int main(){
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    int _=1;
    cin>>_;
    while(_--){
        solve();
    }
    return 0;
}