#include <stdio.h>

int main(void) {
	int n, i, top, s;
	char t[10001];

	scanf("%d", &n);
	while(n--){
		top = i = s = 0;
		scanf("%s", &t);
		while(t[i]){
			if(t[i]==')'){
				if(t[top-1]=='('){
					top--;
				} else {
					printf("NO\n");
					s = -1;
					break;
				}
			} else if(t[i]=='}'){
				if(t[top-1]=='{'){
					top--;
				} else {
					printf("NO\n");
					s = -1;
					break;
				}
			} else if(t[i]==']'){
				if(t[top-1]=='['){
					top--;
				} else {
					printf("NO\n");
					s = -1;
					break;
				}
			} else {
				t[top++] = t[i];
			}
			i++;
		}
		if(s==0){
			if(top==0) printf("YES\n");
			else printf("NO\n");
		}
	}
	return 0;
}