def findr(str, s, e)
  d, i = 0, s
  while i < e
    d+= 1 if str[i] == '('
    d-= 1 if str[i] == ')'
    return i if d == 1 and str[i] == ','
    i += 1
  end
end

def solve(s1, e1, s2, e2)
  f1, f2, m1, m2 = s1 < e1, s2 < e2, e1 - 1, e2 - 2
  m1 = findr($t1, s1, e1) if f1
  m2 = findr($t2, s2, e2) if f2
  if ($isI and f1 and f2) or (not $isI and (f1 or f2))
    printf("("); solve(s1 + 1, m1, s2 + 1, m2)
    printf(","); solve(m1 + 1, e1 - 1, m2 + 1, e2 - 1)
    printf(")")
  end
end

while line = gets
  op, a, b = line.chomp.split(/\s/)
  $t1, $t2 = a.split(//), b.split(//)
  $isI = (op == "i")
  solve(0, $t1.size, 0, $t2.size)
  puts
end