#include <iostream>
#include <map>
#include <stack>
using namespace std;
int main() {
int a;
cin >> a;
map<char, char> m = {{'{','}'}, {'[',']'}, {'(',')'}, {'<','>'}};
map<char, int> level = {{'{',1}, {'[',2}, {'(',3}, {'<',4}};
while (a--) {
string r;
cin >> r;
stack<char> st;
bool valid = true;
for (char ch : r) {
if (ch == '{' || ch == '[' || ch == '(' || ch == '<') {
if (!st.empty() && level[ch] < level[st.top()]) {
valid = false;
break;
}
st.push(ch);
} else {
if (st.empty() || m[st.top()] != ch) {
valid = false;
break;
}
st.pop();
}
}
cout << (valid && st.empty() ? "YES" : "NO") << "\n";
}
return 0;
}