#include <bits/stdc++.h>

//define fin cin
//define fout cout

#define F first
#define S second
#define pii pair < int , int >

using namespace std;

typedef long long ll;

const int nmax = 4000 + 10;

int T , n , i;
vector < char > st;

char s[nmax];

bool deschis(char x)
{
    return (x == '(' || x == '{' || x == '|' || x == '[');
}

bool same(char x , char y)
{
    if (x == '(' && y == ')') return 1;
    if (x == '{' && y == '}') return 1;
    if (x == '[' && y == ']') return 1;
    if (x == '|' && y == '|') return 1;

    return 0;
}

int main()
{
    #ifndef ONLINE_JUDGE
    freopen("input.in","r",stdin);
    freopen("output.out","w",stdout);
    #endif // ONLINE_JUDGE

    /*
    ifstream fin("input.in");
    ofstream fout("output.out");
    */

    for (scanf("%d\n", &T); T ; --T)
    {
        gets(s+1); n = strlen(s+1);

        bool ok = 1;

        st.clear();
        for (i = 1; i <= n && ok; ++i)
        {
            if (s[i] == '|' && st.size() && st.back() == '|')
            {
                st.pop_back();
                continue;
            }

            if (deschis(s[i]))
                st.push_back(s[i]);
            else
            {
                if (st.size() == 0 || !same(st.back() , s[i]))
                    ok = 0;
                else st.pop_back();
            }
        }

        if (st.size() != 0) ok = 0;

        if (ok) printf("YES\n");
        else printf("NO\n");
    }

    return 0;
}