#53763: 答案


s310037@student.cysh.cy.edu.tw (辛吉飛(林恩丞))


for i in range(int(input())):
    s =[]
    for j in input():
        if j in '{(<[':
            s+=[j]
        elif s and s[-1] == '([<{'[')]>}'.find(j)]:
            s.pop()
        else:
            break

    else:
        if not s:
            print('Y')
            continue

    print('N')