
interface Tree{
    fun intersection(other:Tree):Tree
    fun union(other:Tree):Tree
    companion object {
        fun parse(str:String):Tree {
            val (tree, _) = parse(str, 0)
            return tree
        }
        private fun parse(str:String, left:Int):Pair<Tree, Int> {
            return when{
                str[left] == '(' -> {
                    val (leftNode, pos) = parse(str, left + 1)
                    val (rightNode, pos2) = parse(str, pos + 1)
                    Pair(Node(leftNode, rightNode), pos2 + 1)
                }
                else -> Pair(Leaf, left)
            }
        }
    }
}
object Leaf : Tree{
    override fun union(other: Tree): Tree {
        return other
    }

    override fun intersection(other:Tree): Tree {
        return this
    }

    override fun toString(): String {
        return ""
    }
}
class Node constructor(private var leftNode:Tree = Leaf, private var rightNode:Tree= Leaf): Tree {
    override fun intersection(other: Tree): Tree {
        return when(other){
            is Node -> Node(leftNode.intersection(other.leftNode), rightNode.intersection(other.rightNode))
            else -> Leaf
        }
    }

    override fun union(other: Tree): Tree {
        return when(other){
            is Node -> Node(leftNode.union(other.leftNode), rightNode.union(other.rightNode))
            else -> this
        }
    }

    override fun toString(): String {
        return "($leftNode,$rightNode)"
    }
}

fun main(args:Array<String>):Unit {
    var str:String? = readLine()
    while (str != null){
        val (order, a, b) = str.split(' ')
        println(when(order){
            "i" -> Tree.parse(a).intersection(Tree.parse(b))
            else -> Tree.parse(a).union(Tree.parse(b))
        })
        str = readLine()
    }
}
