#include <bits/stdc++.h>
using namespace std;
int n;
string s, z = "<([{", y = ">)]}";
bool pd(string s)
{
stack<char> c;
for (int i = 0; s[i]; i++)
{
if (z.find(s[i]) != string::npos)
{
if (c.empty() || z.find(c.top()) >= z.find(s[i]))
c.push(s[i]);
else
return false;
}
else
{
if (c.empty() || y.find(s[i]) != z.find(c.top()))
return false;
else
c.pop();
}
}
if (c.empty())
return true;
return false;
}
int main()
{
cin >> n;
while (n--)
{
cin >> s;
if (pd(s))
cout << "YES\n";
else
cout << "NO\n";
}
return 0;
}