#The Python 3 solution is as follows:
for i in range(int(input())): #No. of test cases
a=input() #input the actual expression
o=[]
s=""
for j in a:
if(j== '(' ): #ignore the brackets '(' and ')'
pass
elif(ord(j)>=97 and ord(j)<=123): #consider the lower case letters
s=s+str(j)
elif(j== ')' ):
if(len(o)!=0):
s=s+str(o.pop()) #o.pop() removes & returns last item in list
else:
o.append(j) #add the operators to the list
print(s)
#Visit this link for more details.
#Keep visiting for more solutions of SPOJ problems in Python 3.Thank you!!!!
for i in range(int(input())): #No. of test cases
a=input() #input the actual expression
o=[]
s=""
for j in a:
if(j== '(' ): #ignore the brackets '(' and ')'
pass
elif(ord(j)>=97 and ord(j)<=123): #consider the lower case letters
s=s+str(j)
elif(j== ')' ):
if(len(o)!=0):
s=s+str(o.pop()) #o.pop() removes & returns last item in list
else:
o.append(j) #add the operators to the list
print(s)
#Visit this link for more details.
#Keep visiting for more solutions of SPOJ problems in Python 3.Thank you!!!!