{-# LANGUAGE OverloadedStrings #-}

import Control.Applicative
import Data.ByteString.Char8
import qualified Data.ByteString.Char8 as DBC8
import Data.Foldable (toList)
import Data.Sequence (Seq)
import qualified Data.Sequence as S

removeOne :: ByteString -> Seq ByteString -> Seq ByteString
removeOne x seq =
  case S.viewl seq of
    S.EmptyL  -> S.empty
    s S.:< ss ->
      if x == s
         then ss
         else s S.<| (removeOne x ss)

exec :: [ByteString] -> Seq ByteString -> Seq ByteString
exec ["insert", n]   seq = n S.<| seq
exec ["delete", n]   seq = removeOne n seq
exec ["deleteFirst"] seq = let (s  S.:< ss) = S.viewl seq in ss
exec ["deleteLast"]  seq = let (ss S.:>  s) = S.viewr seq in ss

printSeq :: ByteString -> [ByteString] -> IO ()
printSeq _ []       = DBC8.putStrLn "" >> return ()
printSeq sep (x:xs) = do
  DBC8.putStr sep
  DBC8.putStr x
  printSeq " " xs

main :: IO ()
main = do
  n   <- read . DBC8.unpack <$> DBC8.getLine :: IO Int
  seq <- readCommand n S.empty
  printSeq "" (toList seq)
  where
    readCommand :: Int -> Seq ByteString -> IO (Seq ByteString)
    readCommand 0 seq = return seq
    readCommand n seq = do
      cmds <- DBC8.words <$> DBC8.getLine
      readCommand (n-1) (exec cmds seq)

