#include <iostream>
using namespace std;

/*
scanf("%d",&x);
printf("%d\n",x);
*/

const int NMAX=4005;

int n,st[NMAX];
char s[NMAX];

int main() 
{
	int i,j,m,top,ok,nr;
	//freopen("date1.txt","r",stdin);
	//freopen("date2.txt","w",stdout);
	cin>>n;
	for (i=1;i<=n;i++)
	{
		scanf("%s",s+1);
		//printf("%s",s+1);
		top=0;m=strlen(s+1);
		ok=1;nr=0;
		for (j=1;j<=m;j++)
		{
			if (s[j]=='(') st[++top]=1;
			if (s[j]=='[') st[++top]=2;
			if (s[j]=='{') st[++top]=3;
			if (s[j]=='|')
			{
				if (st[top]==4) top--;
				else st[++top]=4;
			}

			if (s[j]==')')
				{
					if (st[top]!=1) ok=0;
					else top--;
				}
			if (s[j]==']')
				{
					if (st[top]!=2) ok=0;
					else top--;
				}
			if (s[j]=='}')
				{
					if (st[top]!=3) ok=0;
					else top--;
				}
		}

		if (top) ok=0;
		if (ok==1) printf("YES\n");
		else printf("NO\n");
	}
	return 0;
}