def extract tree
    stack, arr = [2], [1]
    tree[1..-2].each_char{|c|
        case c
        when "("
            arr << stack[-1]
            stack << stack[-1] * 2
        when ")"
            stack.pop
        when ","
            stack[-1] += 1
        end
    }
    return arr
end

def express numbers
    arr = []
    numbers.each{|x| arr[x]  = "(#{arr[x * 2]},#{arr[x * 2 + 1]})"}
    puts arr[1]
end

while line = gets
    op, tree1, tree2 = line.chomp.split
    node1 = extract(tree1)
    node2 = extract(tree2)
    nodes = (op == "i") ? (node1 - (node1 - node2)).sort.reverse : ((node1 - node2) + node2).sort.reverse
    express nodes
end
