#include<bits/stdc++.h>
#define rep(i,n) for(int i=0;i<(int)(n);i++)
#define pb push_back
#define sz size()
using namespace std;
typedef pair<int,int> pii;

struct Node{
  int id,left,right;
  Node(int a,int b,int c):id(a),left(b),right(c){}
};

pii sim[200];
int shape_id[200];
set<int> shapes[200];
map<pii,int> shape_hash;

inline int construct(const string &s, int &p, vector<Node> &tree){
  if(s[p] == 'x'){
    tree.pb( Node(tree.sz, -1, -1) );
  }else if(s[p] == '('){
    int lid = construct(s, ++p, tree), rid = construct(s, ++p, tree);
    tree.pb( Node(tree.sz, lid, rid) );
  }
  ++p;
  return tree.sz-1;
}

inline void cal_sim(int v, const vector<Node> &tree){
  shapes[v].clear();

  if(tree[v].left<0){
    shape_id[v] = 0;
    shapes[v].insert(0);

    sim[v] = pii(0,1);
  }else{
    int l = tree[v].left, r = tree[v].right;
    cal_sim(l,tree); cal_sim(r,tree);
    for(int x : shapes[l])shapes[v].insert(x);
    for(int x : shapes[r])shapes[v].insert(x);

    pii new_shape = pii(shape_id[l], shape_id[r]);
    if(new_shape.first > new_shape.second)swap(new_shape.first, new_shape.second);
    int new_id;
    if(shape_hash.find(new_shape)!=shape_hash.end())new_id = shape_hash[new_shape];
    else{
      new_id = shape_hash.sz + 1;
      shape_hash[new_shape] = new_id;
    }
    shape_id[v] = new_id;
    shapes[v].insert(new_id);

    int cnt = 0;
    for(int x : shapes[l]){
      if(shapes[r].find(x) != shapes[r].end())cnt++;
    }
    sim[v] = pii(cnt,shapes[v].sz-1);
  }
}

//-1: a<b, 0:a==b, 1: a>b
unordered_map<int,int> memo;
inline int cmp_node(int a, int b, const vector<Node> &tree){
  int key = a*200+b;
  if(memo.count(key))return memo[key];
  int res = 0;

  int dif = sim[a].first*sim[b].second - sim[b].first*sim[a].second;
  if(dif!=0){
    if(dif<0)res = 1;
    else res = -1;
  }else if(tree[a].left<0 && tree[b].left<0){
    res = 0;
  }else{
    int al = tree[a].left, ar = tree[a].right;
    if(cmp_node(al,ar,tree)<0)swap(al,ar);
    int bl = tree[b].left, br = tree[b].right;
    if(cmp_node(bl,br,tree)<0)swap(bl,br);
    
    res = cmp_node(al,bl,tree);
    if(res == 0)res = cmp_node(ar,br,tree);
  }
  return memo[key] = res;
}

inline void transform(int v, vector<Node> &tree, bool is_lc){
  if(tree[v].left<0)return;
  if(is_lc && (cmp_node(tree[v].left, tree[v].right, tree) < 0)){
    swap(tree[v].left, tree[v].right);
  }
  if(!is_lc && (cmp_node(tree[v].left, tree[v].right, tree) > 0)){
    swap(tree[v].left, tree[v].right);
  }
  transform(tree[v].left, tree, true);
  transform(tree[v].right, tree, false);
}

inline string encode(int v, const vector<Node> &tree){
  if(tree[v].left<0)return "x";
  return "(" + encode(tree[v].left,tree) +  " " +  encode(tree[v].right,tree) + ")";
}

int main(){
  cin.tie(0); ios::sync_with_stdio(0);
  string s;
  while(getline(cin,s)){
    if(s == "0")break;
    vector<Node> tree;
    int p = 0;
    construct(s,p,tree);

    shape_hash.clear();
    cal_sim(tree.sz-1,tree);

    memo.clear();
    transform(tree.sz-1,tree,true);

    cout << encode(tree.sz-1,tree) << endl;
  }
}