import java.util.Scanner;
import java.util.Stack;
public class Solution {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
// 테스트케이스
int T = sc.nextInt();
for (int tc = 1; tc <= T; tc++) {
String s = sc.next();
int ans = 0;
Stack<Character> st = new Stack<>();
for (int i = 0; i < s.length(); i++) {
// 여는 기호면 push
if(s.charAt(i)=='(') {
st.push(s.charAt(i));
}
// 닫는 기호면
else {
// 먼저 pop하자
st.pop();
// 닫는 기호 앞이 여는 기호면
// '()' 이런 형태니까 잘라
// 잘라서 ans에 stack 사이즈를 넣어
if(s.charAt(i-1)=='(') {
ans+=st.size();
}
// 닫는 기호 앞이 닫는 기호면
// '))' 이런 형태니까
// ans+1
else {
ans++;
}
}
}
// 답 출력
System.out.printf("#%d %d\n",tc,ans);
}
}
}