#include<bits/stdc++.h>

using namespace std;

#define dbg(x) (cout<<#x<<" = "<<(x)<<'\n')

typedef long long int lld;
const int INF = (1LL << 30) - 1;
const lld LINF = (1LL << 62) - 1;

int N;
char S[4000 + 5];

bool check(char* S) {
	int N = strlen(S);
	vector<char> T;

	for (int i = 0; i < N; i++) {
		if (S[i] == ')' && (T.empty() || T.back() != '(')) return 0;
		else if (S[i] == ')') T.pop_back();

		if (S[i] == ']' && (T.empty() || T.back() != '[')) return 0;
		else if (S[i] == ']') T.pop_back();

		if (S[i] == '}' && (T.empty() || T.back() != '{')) return 0;
		else if (S[i] == '}') T.pop_back();

		if (S[i] == '|' && (!T.empty() && T.back() == '|')) T.pop_back();
		else if (S[i] == '|') T.push_back(S[i]);

		if (S[i] == '(' || S[i] == '[' || S[i] == '{') T.push_back(S[i]);
	}

	return T.empty();
}

int main() {
	cin.sync_with_stdio(false);

	#ifndef ONLINE_JUDGE
	freopen("input.txt", "r", stdin);
	freopen("output.txt", "w", stdout);
	#endif

	scanf("%d", &N);

	while (N--) {
		scanf("%s", S);
		printf("%s\n", check(S) ? "YES" : "NO");
	}

	return 0;
}