#include <bits/stdc++.h>

using namespace std;

bool isValid(char a, char b) {
	return a == '(' && b == ')' ||
		   a == '[' && b == ']' ||
		   a == '{' && b == '}' ||
		   a == '|' && b == '|';
}

int main() {
	int n; cin >> n;
	while (n--) {
		stack<char> st;
		string s; cin >> s;
		for (int i = 0; i < s.size(); ++i) {
			if (!st.empty() && isValid(st.top(), s[i])) {
				st.pop();
			} else {
				st.push(s[i]);
			}
		}
		if (st.empty()) {
			cout << "YES\n";
		} else {
			cout << "NO\n";
		}
	}
	return 0;
}