program expr; var stack : string[255]; t : string[255]; u : string[255]; i : integer; cases : integer; ch : char; procedure pushs (ch: char); begin if (length (stack) > 253) then while (true) do; stack := stack + ch; end; function pops: char; var ch : char; begin if (length (stack) = 0) then while (true) do; ch := stack[length(stack)]; delete (stack, length (stack), 1); pops := ch; end; procedure pushu (ch: char); begin if (ch <> '(') then u := u + ch; end; function popu (up: char; right: boolean): string; var ch : char; s : string[255]; doit : boolean; begin ch := u[length(u)]; delete (u, length(u), 1); s := ch; if ch in ['+', '-', '*', '/'] then begin s := s + popu (ch, true); s := popu (ch, false) + s; doit := false; case up of '-' : if right and (ch in ['-', '+']) then doit := true; '/' : if right or (ch in ['-', '+']) then doit := true; '*' : if ch in ['-', '+'] then doit := true; end; if doit then s := '(' + s + ')'; end; popu := s; end; begin readln (cases); while (cases > 0) do begin readln (t); t := '(' + t + ')'; stack:=''; u:=''; for i:=1 to length (t) do begin case t[i] of '(' : pushs ('('); '*','/' : begin if stack[length(stack)] in ['*', '/'] then pushu (pops); pushs(t[i]); end; '+','-' : begin if stack[length(stack)] in ['+', '-', '*', '/'] then pushu (pops); pushs(t[i]); end; ')' : repeat ch := pops; pushu(ch); until ch='('; else pushu (t[i]); end; end; writeln (popu (' ', false)); dec (cases); end; end.