require 'set'

### global variables

$node = [Rational(0), Set.new(['x']), 'x']

### subroutines

def asym?(tr0, tr1)
  sim0, set0, sk0, tr00, tr01 = tr0
  sim1, set1, sk1, tr10, tr11 = tr1

  # 1
  return sim1 <=> sim0 if sim0 != sim1

  # 2
  return 0 if tr00.nil? && tr01.nil? && tr10.nil? && tr11.nil?

  if asym?(tr00, tr01) >= 0
    tr0s = tr00
    tr0w = tr01
  else
    tr0s = tr01
    tr0w = tr00
  end

  if asym?(tr10, tr11) >= 0
    tr1s = tr10
    tr1w = tr11
  else
    tr1s = tr11
    tr1w = tr10
  end

  # 3
  cmp_s = asym?(tr0s, tr1s)
  return cmp_s if cmp_s != 0

  # 4
  cmp_w = asym?(tr0w, tr1w)
  return cmp_w if cmp_w != 0

  # 5
  0
end

# <tree> := 'x' | '(' <tree> ' ' <tree> ')'

def parse_tree(str)
  if str[$pos] == 'x'
    $pos += 1
    return $node
  end

  $pos += 1
  tr0 = parse_tree(str)
  $pos += 1
  tr1 = parse_tree(str)
  $pos += 1

  set0 = tr0[1]
  set1 = tr1[1]

  set_c = set0 & set1
  set_u = set0 | set1
  sim = Rational(set_c.length, set_u.length)

  cmp = asym?(tr0, tr1)
  if cmp < 0
    tmp = tr0; tr0 = tr1; tr1 = tmp
  end

  sk = [tr0[2], tr1[2]]
  [sim, set_u | [sk], sk, tr0, tr1]
end

def arrange_tree(tr, rev = false)
  return 'x' if tr == $node

  trl = tr[3]
  trr = tr[4]

  if rev
    tmp = trl; trl = trr; trr = tmp
  end

  atrl = arrange_tree(trl, false)
  atrr = arrange_tree(trr, true)

  "(#{atrl} #{atrr})"
end

### main

loop do
  str = gets.strip
  break if str == '0'

  $pos = 0
  tr = parse_tree(str)
  #p tr[2]
  puts arrange_tree(tr)
end