import sys
n = int(input())
data = iter(sys.stdin.read().splitlines())
dic ={"{":"}","[":"]","(":")","<":">"}
cmp_key ={"{":"4","[":"3","(":"2","<":"1"}
for _ in range(n):
flag = True
stack = []
s = next(data)
for c in s:
if c in cmp_key: # 左括号判断
if stack:
top = stack[-1] # 直接取栈顶,无需弹出再放回
if cmp_key[stack[-1]] < cmp_key[c]:
flag = False
break
stack.append(c)
else: # 右括号
if not stack: # 补充:栈空遇到右括号,直接不合法
flag = False
break
if dic[stack.pop()] != c:
flag = False
break
# 统一在循环结束后输出,避免重复打印
print("YES" if flag and len(stack) == 0 else "NO")