from sys import stdin

MATCH = { "|": "|", "}" : "{", ")" : "(", "]": "[" }
tests = int(stdin.readline())

for test in xrange(tests):
    line = stdin.readline().rstrip()

    stack = []
    for c in line:
        if len(stack) > 0 and c in MATCH.keys():
            if MATCH[c] == stack[-1]:
                del stack[-1]
                continue
        stack += [c]

    if len(stack) == 0:
        print "YES"
    else:
        print "NO"