import Control.Monad
import Control.Applicative
import Debug.Trace
import System.IO

data Tree = Branch Tree Tree | Leaf
          deriving (Eq)
instance Show Tree where
  show (Branch l1 r1) = "("++show l1 ++ ","
                        ++show r1 ++ ")"
  show Leaf = ""

main :: IO ()
main = do
  ss <- (map words . lines) <$> getContents
--  mapM_ (print . map toTree . drop 1) ss
  mapM_ (print . solve) ss
--s <- getLine
--  print (toTree s)

solve :: [String] -> Tree
solve (q:s1:s2:_) = if q == "u" then union' t1' t2'
                    else inter' t1' t2'
  where
    t1' = toTree s1
    t2' = toTree s2
    union' t1 t2 =
      case (t1, t2) of
        ((Branch l1 r1),(Branch l2 r2)) ->
          Branch (union' l1 l2) (union' r1 r2)
        (_, Leaf) -> t1
        (Leaf, _) -> t2
    inter' t1 t2 =
      case (t1, t2) of
        ((Branch l1 r1), (Branch l2 r2)) ->
          Branch (inter' l1 l2) (inter' r1 r2)
        (_, _) -> Leaf
        
toTree :: String -> Tree
toTree str = fst $ go str
  where
    go :: String -> (Tree, String)
--    go [] = (Leaf, [])
    go (s:ss)
      | s == '(' = let (l, _:ss') = go ss
                       (r, _:ss'') = go ss' in
                     (Branch l r, ss'')
      | s == ',' = (Leaf, s:ss)
      | s == ')' = (Leaf, s:ss)