{  ACM International Collegiate Programming Contest
   Central European Regional Contest 2000
   
   Problem E - Complicated Expressions
   Martin Kacer
}

Program Parentheses;
Const MAX_EXPR = 16384;
Var TaskCnt, TaskNum : Integer;
	NumChars, I, J	 : Integer;
	Expr			 : Array [1..MAX_EXPR] of Char;
	Match			 : Array [1..MAX_EXPR] of Integer;
	Useless			 : Array [1..MAX_EXPR] of Boolean;
	InsideMult		 : Array [1..MAX_EXPR] of Boolean;
	InsidePlus		 : Array [1..MAX_EXPR] of Boolean;
	LevelLeft		 : Array [0..(MAX_EXPR div 2)] of Integer;
	LevelNum		 : Integer;
Begin
	Readln (TaskCnt);
	For TaskNum:= 1 to TaskCnt do
	Begin
		Expr[1]:= '+';   { left sentinel }
		NumChars:= 1;
		While not Eoln do Begin
			NumChars:= NumChars + 1;
			Read (Expr[NumChars]);
		End;
		ReadLn;
		NumChars:= NumChars + 1;
		Expr[NumChars]:= '+';   { right sentinel }

		{***** find matching pairs *****}
		LevelNum:= 0;
		For I:= 1 to NumChars do
		Begin
			Match[I]:= 0;
			If (Expr[I] = '(') then Begin
				LevelNum:= LevelNum + 1;
				LevelLeft[LevelNum]:= I;
			End else If (Expr[I] = ')') then Begin
				Match[LevelLeft[LevelNum]]:= I;
				Match[I]:= LevelLeft[LevelNum];
				LevelNum:= LevelNum - 1;
			End;
		End;

		{***** eliminate multiple parentheses *****}
		J:= 0;
		For I:= 1 to NumChars do
		Begin
			J:= J + 1;
			Expr[J]:= Expr[I];
			If (Expr[I] = '(') then Begin
				If (Match[I] = Match[I+1]+1) then
					J:= J - 1;
			End else If (Expr[I] = ')') then Begin
				If (Match[I] = Match[I-1]-1) then
					J:= J - 1;
			End;
		End;
		NumChars:= J;

		{***** find operators inside parentheses *****}
		LevelNum:= 0;
		LevelLeft[0]:= 1;
		For I:= 1 to NumChars do
		Begin
			Match[I]:= 0;
			InsidePlus[I]:= false;
			InsideMult[I]:= false;
			If (Expr[I] = '(') then Begin
				LevelNum:= LevelNum + 1;
				LevelLeft[LevelNum]:= I;
			End else If (Expr[I] = ')') then Begin
				Match[LevelLeft[LevelNum]]:= I;
				Match[I]:= LevelLeft[LevelNum];
				LevelNum:= LevelNum - 1;
			End else If (Expr[I] = '*') or (Expr[I] = '/') then
				InsideMult[LevelLeft[LevelNum]]:= true
			else If (Expr[I] = '+') or (Expr[I] = '-') then
				InsidePlus[LevelLeft[LevelNum]]:= true;
		End;

		{***** determine useless parentheses *****}
		For I:= 1 to NumChars do
			If (Expr[I] = '(') then Begin
				If not InsidePlus[I] and not InsideMult[I] then
					Useless[I]:= true
				else If (not InsidePlus[I]) then
					Useless[I]:= (Expr[I-1] <> '/')
				else
					Useless[I]:= (Expr[I-1] in ['+','('])
							and (Expr[Match[I]+1] in ['+','-',')']);
				Useless[Match[I]]:= Useless[I];
			End else If (Expr[I] <> ')') then
				Useless[I]:= false;

		{***** delete useless parentheses *****}
		J:= 0;
		For I:= 1 to NumChars do
		Begin
			J:= J + 1;
			Expr[J]:= Expr[I];
			If (Useless[I]) then J:= J - 1;
		End;
		NumChars:= J;

		For I:= 2 to NumChars-1 do Write (Expr[I]);
		WriteLn;
	End;
End.
