### subroutines

def make_tree(st)
  return nil if st[0] != '('

  st[0] = ''
  lt = make_tree(st)
  puts "error ," if st[0] != ','
  st[0] = ''
  rt = make_tree(st)
  puts "error )" if st[0] != ')'
  st[0] = ''

  [lt, rt]
end

def intersection(t0, t1)
  return nil if t0.nil? || t1.nil?

  [ intersection(t0[0], t1[0]), intersection(t0[1], t1[1])]
end

def union(t0, t1)
  return t1 if t0.nil?
  return t0 if t1.nil?

  [ union(t0[0], t1[0]), union(t0[1], t1[1])]
end

def tree2str(t)
  return '' if t.nil?
  '(' + tree2str(t[0]) + ',' + tree2str(t[1]) + ')'
end

### main

while line = gets
  op, st0, st1 = line.strip.split(' ')

  t0 = make_tree(st0)
  t1 = make_tree(st1)
  #p t0
  #p t1

  t = (op == 'i') ? intersection(t0, t1) : union(t0, t1)
  puts tree2str(t)
end