https://www.acmicpc.net/problem/9012

// '(' 일때 push를 하고 ')'일때 pop을 한다.
// stack이 비어있을때 ')' 이 나오면 push 하고 break문을 걸어 바로 NO가 나오게 한다.
// 최종적으로 stack이 비어있으면 -> YES
// 비어있지 않으면 -> NO
import java.util.*;
import java.io.*;
public class Main {
public static void main(String[] args) throws Exception {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int N = Integer.parseInt(br.readLine()); // 괄호 문자열의 길이 N
Stack<Character> stack = new Stack<>();
for(int i=0; i<N; i++) {
String str = br.readLine();
int length = str.length();
for(int j=0; j<length; j++) {
char ch = str.charAt(j);
if(ch == '(') {
stack.push(ch); // '(' 일때 push
}
else {
int size = stack.size();
if(size == 0) {
stack.push(ch); //stack이 비어있을때 ')' 이 나오면 push
break;
}
else {
stack.pop();
}
}
}
if(stack.isEmpty()) {
System.out.println("YES");
}
else {
System.out.println("NO");
}
stack.clear();
}
}