n=int(input())
for i in range(n):
word=input();count=0;stack=[]
while word[-1]==" ":
word=word[0:len(word)-1]
#print("word",word)
for j in range(len(word)):
#print("要加入",word[j])
if (word[j]!="") :
if (len(stack)!=0 and stack[-1]=="(" and word[j]==")" ):
stack.pop()
count+=1
elif (len(stack)!=0 and stack[-1]=="<" and word[j]==">" ):
stack.pop()
count+=1
elif (len(stack)!=0 and stack[-1]=="{" and word[j]=="}" ):
stack.pop()
count+=1
else:
stack.append(word[j])
#print(stack,count)
#這樣子的寫法很神奇
if (len(stack)!=0):
if stack==['[', ']'] or stack==['{', '}'] or stack==['<', '>'] or stack==['(', ')'] or stack==[]:count+=1;print("Y")
else:
count=0;print("N")