#include <iostream>
#include <stack>
#include <string>
#include <cstring>
#include <vector>
using namespace std;
int main()
{
	int n,j;
	cin >> n;
	vector<string> res;
	for (j = 1;j <= n;j++)
	{
		stack<char> st;
		string str;
		char c;
		cin >> str;
		int i;
		for (i = 0;i < str.length();i++)
		{
			c = str[i];
			if (!st.empty())
			{
				char top = st.top();
				if (c == '|' && top == '|')
					st.pop();
				else if (c == '}' && top == '{')
					st.pop();
				else if (c == ')' && top == '(')
					st.pop();
				else if (c == ']' && top == '[')
					st.pop();
				else
					st.push(c);
			}
			else
				st.push(c);
		}
		if (st.empty())
			 res.push_back("YES\n");
		else
			res.push_back("NO\n");
	}
	for (auto s : res)
		cout << s;
	system("pause");
	return 0;
}