n=int(input());stack=[]
for i in range(n):
word=input();count=0
for j in range(len(word)-1):
if (len(stack)!=0 and stack[-1]=="(" and word[j]==")" ):
stack.pop()
count+=1
else:
stack.append(word[j])
if (len(stack)!=0):count=0
print(count)
n=int(input())
for i in range(n):
word=input();count=0;stack=[]
#for a in range(len(word)):
while word[-1]=="":
word=word[0:len(word)-1]
print("word",word)
for j in range(len(word)):
print("要加入",word[j])
if word[j]==" ":pass
if (word[j]!="") :
if (len(stack)!=0 and stack[-1]=="(" and word[j]==")" ):
stack.pop()
count+=1
else:
stack.append(word[j])
print(stack)
#刪除陣列中的空白
#這樣子的寫法很神奇
if (len(stack)!=0) :count=0
print(count)