class Node
  attr_reader :left, :right
  def initialize(left,right)
    @left = left
    @right = right
  end
  def leaf?
    not (left || right)
  end
  def to_s
    if self.leaf?
      "(,)"
    elsif !@left
      "(,#@right)"
    elsif !@right
      "(#@left,)"
    else
      "(#@left,#@right)"
    end
  end
end

class Parser
  def initialize(str)
    @str = str
    @idx = 0
  end
  def read
    if @str[@idx] == "("
      @idx += 1
      left = read
      @idx += 1
      right = read
      @idx += 1
      Node.new(left,right)
    else
      nil
    end
  end
end

def its (a,b)
  if a && b
    Node.new(its(a.left,b.left),its(a.right,b.right))
  else
    nil
  end
end

def uni (a,b)
  if !a
    b
  elsif !b
    a
  else
    Node.new(uni(a.left,b.left),uni(a.right,b.right))
  end
end

while line = gets
  ch, a, b = line.split
  c = Parser.new(a).read
  d = Parser.new(b).read

  if ch=="i"
    puts its(c,d)
  else
    puts uni(c,d)
  end
end