#P7794. Parentheses Matching

Parentheses Matching

Parentheses Matching

Problem Description

Given a string PP consisting of only parentheses and asterisk characters (i.e. "(", ")" and ""), you are asked to replace all the asterisk characters in order to get a balanced parenthesis string with the shortest possible length, where you can replace each "" by one "(", or one ")", or an empty string "". A parenthesis string SS is a string consisting of only parentheses (i.e. "(" and ")"), and is considered balanced if and only if:

SS is an empty string, or

● there exist two balanced parenthesis strings AA and BB such that S=ABS = A B, or

● there exists a balanced parenthesis string CC such that S=(C)S = (C).

For instance, "", "()", "(())", "()()", "()(())" are balanced parenthesis strings. Due to some notorious technical inability, if there are several solutions with the shortest possible length, then you have to report the smallest possible one in lexicographical order. For every two different strings AA and BB of the same length nn, we say AA is smaller than BB in lexicographical order if and only if there exists some integer kk such that:

1kn1 \leq k \leq n, and

● the first (k1)(k - 1) characters of AA and that of BB are exactly the same, and

● the kk-th character of AA is smaller than that of BB.

For instance, "()(())" is smaller than "()()()", and in this case, k=4k = 4.

Input

There are several test cases. The first line contains an integer TT (1T1051 \leq T \leq 10^5), denoting the number of test cases. Then follow all the test cases. For each test case, the only line contains a string of length nn (1n1051 \leq n \leq 10^5), denoting the string PP that consists of only parentheses and asterisk characters. It is guaranteed that the sum of nn in all test cases is no larger than 5×1065 \times 10^6.

Output

For each test case, output in one line "No solution!" (without quotes) if no solution exists, or otherwise the smallest possible solution in lexicographical order. Note that the output characters are case-sensitive.

Sample Input

5
*))*)
*(*)*
*)*(*
******
((***)()((**

Sample Output

No solution!
()
()()
(())()(())

Source

2020 Multi-University Training Contest 3