



처음엔 "(" = -1, ")" = 1 로 정의하고 sum() = 0으로 풀으려고 했는데 순서가 뒤바뀌더라도 나오는 횟수가 같으면 결국은 0이 되므로 잘못된 생각이었다.
import sys
input = sys.stdin.readline
t = int(input())
for _ in range(t):
test = input().strip()
stack = []
is_vps = True
for i in test:
if i == '(':
stack.append(i)
else:
if stack:
stack.pop()
else:
is_vps = False
if is_vps and not stack:
print('YES')
else:
print('NO')