#include<bits/stdc++.h>
using namespace std;
using Int = long long;
signed main(){  
  cin.tie(0);
  ios::sync_with_stdio(0);
  string s;
  while(getline(cin,s),s!="0"){
    Int m=count(s.begin(),s.end(),'x')*2-1;
    if(m==1){
      cout<<s<<endl;
      continue;
    }
    
    Int cur=0;
    vector<Int> L(m,-1),R(m,-1);
    
    function<Int(string)> dfs=[&](string s){
      //cout<<s<<endl;
      Int v=cur++;
      if(s=="x"){
	return v;
      }
      Int op=0;
      for(Int i=0;i<(Int)s.size();i++){
	if(s[i]=='(') op++;
	if(s[i]==')') op--;
	if(op==1&&s[i]==' '){
	  string ls=s.substr(1,i-1);
	  string rs=s.substr(i+1,s.size()-(i+2));
	  L[v]=dfs(ls);
	  R[v]=dfs(rs);
	  return v;
	}
      }
    };
    dfs(s);

    vector<string> sh(m);
    vector<set<string> > dp(m);
    vector<Int> p(m),q(m);
    vector<Int> p1(m),q1(m);
    vector<Int> p2(m),q2(m);

    auto check=[&](Int a,Int b,Int c,Int d){
      return a*d>c*b;
    };
    
    function<void(Int)> dfs2=[&](Int v){
      if(L[v]<0){
	p[v]=0;q[v]=1;
	sh[v]="()";
	dp[v].emplace(sh[v]);
	return;
      }
      dfs2(L[v]);
      dfs2(R[v]);
      
      set<string> uni;
      dp[v]=dp[R[v]];
      for(auto ss:dp[L[v]]){
	dp[v].emplace(ss);
	if(dp[R[v]].count(ss))
	  uni.emplace(ss);
      }
      p[v]=uni.size();
      q[v]=dp[v].size();
      
      sh[v]="("+(sh[L[v]]<sh[R[v]]?sh[L[v]]+sh[R[v]]:sh[R[v]]+sh[L[v]])+")";      
      dp[v].emplace(sh[v]);

      p1[v]=L[v];p2[v]=R[v];
    };
    dfs2(0);

    function<void(Int, Int)> dfs4=[&](Int v,Int k){
      if(L[v]<0) return;
      function<Int(Int,Int)> ccc=[&](Int a,Int b)->Int{
	if(check(p[a],q[a],p[b],q[b])){
	  return 1;
	}
	
	if(check(p[b],q[b],p[a],q[a])){
	  return 0;
	}
	
	if(!p[a]&&!p[b]){
	  return 0;
	}

	if(ccc(p1[a],p2[a])){
	  swap(p1[a],p2[a]);
	}

	if(ccc(p1[b],p2[b])){
	  swap(p1[b],p2[b]);
	}
	
	if(ccc(p1[a],p1[b])){
	  return 1;
	}
	
	if(ccc(p1[b],p1[a])){
	  return 0;
	}
	
	if(ccc(p2[a],p2[b])){
	  return 1;
	}
	
	return 0;
      };
      if(k^ccc(L[v],R[v])) swap(L[v],R[v]);
      dfs4(L[v],0);
      dfs4(R[v],1);
    };
    dfs4(0,0);

    stringstream ss;
    function<void(Int)> dfs3=[&](Int v){
      if(L[v]<0){
	ss<<"x";
	return;
      }
      ss<<"(";
      dfs3(L[v]);
      ss<<" ";
      dfs3(R[v]);
      ss<<")";
    };
    dfs3(0);
    cout<<ss.str()<<endl;
  }
  return 0;
}


