### constant

DXYS = [[0, 0], [-1, 0], [0, -1], [0, 1], [1, 0]]

### subroutines

def conflict?(d, h, lw, llines, rw, rlines)
  for y in (0...h)
    for x in (0...rw)
      next if rlines[y][x] == ' '
      x0 = x + d
      DXYS.each do |dx, dy|
        x1 = x0 + dx
        y1 = y + dy
        if x1 >= 0 && x1 < lw && y1 >= 0 && y1 < h
          l = llines[y1][x1]
          return true if l && l != ' '
        end
      end
    end
  end
  false
end

def merge(ld, rd, h, lw, llines, rw, rlines)
  w = [lw + ld, rw + rd].max
  lines = h.times.map{' ' * w}

  for y in (0...h)
    for x in (0...lw)
      ch = llines[y][x]
      if ch && ch != ' '
        lines[y][x + ld] = ch
      end
    end
    for x in (0...rw)
      ch = rlines[y][x]
      if ch && ch != ' '
        lines[y][x + rd] = ch
      end
    end
  end

  lines
end

### <but> := <l> | <l> '(' <but> ')' | <l> '(' <but> ',' <but> ')'
### <l> := 'A'..'Z' | 'a'..'z'

### [ h, w, x, [lines,..]]

def parse(str)
  root = str[$pos]
  $pos += 1

  if str[$pos] != '('
    return [1, 1, 0, [root]]	# 0-ary
  end
  $pos += 1

  llf = parse(str)
  lh, lw, lx, llines = llf

  ch = str[$pos]
  $pos += 1
  if ch == ')'
    sep = ' ' * lw
    sep[lx] = '-'
    bline = ' ' * lw
    bline[lx] = root
    return [lh + 2, lw, lx, llines + [sep, bline]]
  end

  rlf = parse(str)
  rh, rw, rx, rlines = rlf

  $pos += 1

  llines0 = llines.clone
  rlines0 = rlines.clone
  h = lh
  if lh < rh
    llines0 = [' ' * lw] * (rh - lh) + llines0
    h = rh
  elsif lh > rh
    rlines0 = [' ' * rw] * (lh - rh) + rlines0
    h = lh
  end

  rd = lx + 2 - rx
  rd = 0 if rd < 0
  while conflict?(rd, h, lw, llines0, rw, rlines0)
    rd += 1
  end

  ld = 0
  if rd == 0
    while ! conflict?(ld, h, rw, rlines0, lw, llines0)
      ld += 1
    end
    ld -= 1
  end

  lines = merge(ld, rd, h, lw, llines0, rw, rlines0)
  w = lines[0].length
  
  slen = rx + rd - (lx + ld) + 1
  x = (lx + ld) + (slen - 1) / 2

  sep = ' ' * w
  sep[lx + ld, slen] = '-' * slen
  bline = ' ' * w
  bline[x] = root

  [h + 2, w, x, lines + [sep, bline]]
end

### main

strs = ''

loop do
  line = gets.strip
  strs += line.gsub(/\s/, '')
  break if line.include?('.')
end

strs.split(/[;\.]/).each_with_index do |str, id|
  #p str

  $pos = 0
  but = parse(str)
  #p but

  h, w, x, lines = but
  #p [h, w, x]

  puts "#{id + 1}:"
  lines.each{|l| puts l.rstrip}
end