A valid parentheses sequence is a non-empty string where each character is either '(' or ')', which satisfies the following constraint:

You can find a way to repeat erasing adjacent pairs of parentheses '()' until it becomes empty.

	For example, '(())' and '()((()()))' are valid parentheses sequences, but ')()(' and '(()' are not.

	Mike has a valid parentheses sequence. He really likes everything about his sequence, except the fact that it is quite long. So Mike has recently decided that he will replace his parentheses sequence with a new one in the near future. But not every valid parentheses sequence will satisfy him. To help you understand his requirements we'll introduce the pseudocode of function F(S):

	FUNCTION F( S - a valid parentheses sequence )
	BEGIN
		balance = 0
		max_balance = 0
		FOR index FROM 1 TO LENGTH(S)
		BEGIN
			if S[index] == '(' then balance = balance + 1
			if S[index] == ')' then balance = balance - 1
			max_balance = max( max_balance, balance )
		END
		RETURN max_balance
	END

	In other words, F(S) is equal to the maximal balance over all prefixes of S.

	Let's denote A as Mike's current parentheses sequence, and B as a candidate for a new one. Mike is willing to replace A with B if F(A) is equal to F(B). He would also like to choose B with the minimal possible length amongst ones satisfying the previous condition. If there are several such strings with the minimal possible length, then Mike will choose the least one lexicographically, considering '(' to be less than ')'.

	Help Mike!

-----Input-----

	The first line of the input contains one integer T denoting the number of testcases to process.

	The only line of each testcase contains one string A denoting Mike's parentheses sequence. It is guaranteed that A only consists of the characters '(' and ')'. It is also guaranteed that A is a valid parentheses sequence.

-----Output-----

	The output should contain exactly T lines, one line per each testcase in the order of their appearance. The only line of each testcase should contain one string B denoting the valid parentheses sequence that should be chosen by Mike to replace A.

-----Constraints-----
1 ≤ T ≤ 5;
1 ≤ |A| ≤ 100000(105).

-----Example-----
Input:
1
()((()()))

Output:
((()))

Difficulty: interview

Starter code:

Solutions:
try:
    for i in range(int(input())):
        s=input()
        balance=0
        max_balance=0
        for i in s:
            if i=='(':balance+=1
            else:
                balance-=1
            max_balance=max(max_balance,balance)
        print('('*max_balance,')'*max_balance,sep="")
except Exception as e:
    print(e)
        

# cook your dish here
for _ in range(int(input())):
    b = 0
    mx = 0
    for i in input():
        if i == "(":
            b += 1
        else:
            b -= 1
        mx = max(b,mx)
    print("("*mx + ")"*mx)

# cook your dish here
def F(S):
	balance=0
	max_balance=0
	for i in S:
		if (i=='('):
		    balance+=1
		elif(i== ')'):
		    balance-=1
		max_balance=max(max_balance,balance)
	
	return max_balance

for _ in range(int(input())):
    x=input()
    maxi=F(x)
    tot='('*maxi
    tot+=')'*maxi
    print(tot)

# cook your dish here
def fun(s):
    bal=0
    maxbal=0
    for i in range(len(s)):
        if s[i]=='(':
            bal+=1
        else:
            bal-=1
        maxbal=max(maxbal,bal)
    return maxbal
t=int(input())
while t>0:
    s=input()
    maxbal=fun(s)
    print('('*maxbal+')'*maxbal)
    t-=1

# cook your dish here
for s in range(int(input())):
    A=input()
    balance=0
    max_balance=0
    for i in range(len(A)):
        if(A[i]=='('):
            balance+=1
        if(A[i]==')'):
            balance-=1
        max_balance=max(max_balance,balance)
    for i in range(max_balance):
        print("(",end='')
    for i in range(max_balance):
        print(")",end='')
    print()

for i in range(int(input())):
    j=input()
    balance = 0
    max_balance = 0
    for i in range(len(j)):
        if j[i] == '(' :balance+=1
        if j[i] == ')' :balance-=1
        max_balance = max(max_balance, balance)
    for i in range(max_balance):
        print("(",end='')
    for i in range(max_balance):
        print(")",end='')
    print()

try:
    for i in range(int(input())):
        st = input()
        balance = 0
        max_balance = 0
        for i in st:
            if i=='(': balance+=1
            else: balance-=1
            max_balance = max(max_balance, balance)
        print('('*max_balance,')'*max_balance, sep="")
except Exception as e:
    print(e)

try:
    for i in range(int(input())):
        st = input()
        balance = 0
        max_balance = 0
        for i in st:
            if i=='(': balance+=1
            else: balance-=1
            max_balance = max(max_balance, balance)
        print('('*max_balance,')'*max_balance, sep="")
except Exception as e:
    print(e)

# cook your dish here
# cook your dish here
try:
    
    def F(S):
        balance = 0
        max_balance = 0
        for i in S:
            if i == '(':
                balance += 1 
            elif i == ')':
                balance -= 1 
            max_balance = max(balance, max_balance)
        return max_balance
    
    for _ in range(int(input())):
        S = input()
        max_balance = F(S)
        for i in range(max_balance):
            print('(', end='')
        for i in range(max_balance):
            print(')', end='')
        print('')
    
except:
    pass


# cook your dish here
def F(S):
	balance=0
	max_balance=0
	for i in S:
		if (i=='('):
		    balance+=1
		elif(i== ')'):
		    balance-=1
		max_balance=max(max_balance,balance)
	
	return max_balance

for _ in range(int(input())):
    S=input()
    max_balance=F(S)
    resStr='('*max_balance
    resStr+=')'*max_balance
    print(resStr)# cook your dish here


# cook your dish here
def F(S):
	balance=0
	max_balance=0
	for i in S:
		if (i=='('):
		    balance+=1
		elif(i== ')'):
		    balance-=1
		max_balance=max(max_balance,balance)
	
	return max_balance

for _ in range(int(input())):
    S=input()
    max_balance=F(S)
    resStr='('*max_balance
    resStr+=')'*max_balance
    print(resStr)


for i in range(int(input())):
    s = input()
    c = 0
    mc = 0
    for i in s:
        if i == "(":
            c+=1
        else:
            c-=1
        mc = max(mc,c)
    print("("*mc+")"*mc)

# cook your dish here
try:
    
    def F(S):
        balance = 0
        max_balance = 0
        for i in S:
            if i == '(':
                balance += 1 
            elif i == ')':
                balance -= 1 
            max_balance = max(balance, max_balance)
        return max_balance
    
    for _ in range(int(input())):
        S = input()
        max_balance = F(S)
        for i in range(max_balance):
            print('(', end='')
        for i in range(max_balance):
            print(')', end='')
        print('')
    
except:
    pass


# cook your dish here
for i in range(int(input())):
    s = input()
    c = 0
    mc = 0
    for i in s:
        if i == "(":
            c+=1
        else:
            c-=1
        mc = max(mc,c)
    print("("*mc+")"*mc)

T = int(input())
for j in range(0,T):
    s= input()
    maxbalance = 0
    balance = 0
    for i in range(len(s)):
        a = s[i]
        if a == '(':
            balance += 1
        else:
            balance -= 1
        if balance > maxbalance:
            maxbalance = balance
    print('('*maxbalance,')'*maxbalance,sep = '')

# cook your dish here
for _ in range(int(input())):
    s = input()
    maxcount = 0
    count = 0
    for i in s:
        if i == '(':
            count+=1
        else:
            count-=1
        maxcount = max(maxcount,count)
    print('('*maxcount+')'*maxcount)

# cook your dish here
for _ in range(int(input())):
    s = input()
    maxcount = 0
    count = 0
    for i in s:
        if i == '(':
            count+=1
        else:
            count-=1
        maxcount = max(maxcount,count)
    print('('*maxcount+')'*maxcount)

# cook your dish here
for _ in range(int(input())):
    maxcount,count=0,0
    s = input()
    for i in s:
        if i == '(':
            count += 1
        else:
            count -= 1
        maxcount = max(maxcount,count)
    print('('*maxcount+')'*maxcount)

for i in range(int(input())):
    x=input()
    maxlen=0
    balance=0
    for i in x:
        if i=='(':
            balance+=1
        else:
            balance-=1
        maxlen=max(maxlen,balance)
    print('('*maxlen +')'*maxlen)