#include <functional>
#include <iostream>
using namespace std;

void solve(){
    string s;cin>>s;
    string t = "";

    auto check = [&](char a, char b) {
        if(a == '[' && b == '{' ||
            a == '(' && b == '{' || 
            a == '(' && b == '[' ||
            a == '<' && b == '{' ||
            a == '<' && b == '[' ||
            a == '<' && b == '(' 
        ){
            return -1;
        }
        else if(a == '(' && b == ')' ||
            a == '[' && b == ']' || 
            a == '{' && b == '}' ||
            a == '<' && b == '>'
        ) return 1;
        else return 0;
    };

    for(char i : s){
        if(t.size() == 0) t += i;
        else{
            bool f = check(t.back(), i);
            if(f == true){
                t.erase(t.end() - 1);
            }else if(f == false){
                t += i;
            }else{
                cout<<"NO\n";
            }
        }
    }
    if(t.size()) cout<<"NO\n";
    else cout<<"YES\n";
}

int main() {
    int t;cin>>t;
    while(t--){
        solve();
    }
}
// 64 位输出请用 printf("%lld")