#!/bin/python

import sys

def calculate(s):    
    dict = {'{':'}','[':']','(':')'}
    stack =[]
    balanced = 0
    for i in s:
        if i in ['{','[','(']:
            stack.append(i)
        else:
            if i in ['}',']',')']:
                if not stack:
                    return 'NO'
                a = stack.pop()
                if dict[a]== i:
                    balanced = 1
                else: 
                    return 'NO'
    if stack:
        return 'NO'                       
    if balanced == 1:
        return 'YES'


t = int(raw_input().strip())
for _ in range(t):
    print(calculate(raw_input().strip()))

results matching ""

    No results matching ""