import Control.Applicative
import Data.Array.IO

main :: IO ()
main = do
  n <- read <$> getLine :: IO Int
  key  <- newArray(0, 2100000) 0 :: IO(IOUArray Int Int)
  next <- newArray(0, 2100000) 0 :: IO(IOUArray Int Int)
  prev <- newArray(0, 2100000) 0 :: IO(IOUArray Int Int)
  solve n key next prev

solve :: Int -> IOUArray Int Int -> IOUArray Int Int -> IOUArray Int Int -> IO ()
solve n key next prev = do
  if n == 0 then
    do {
      printanswer key next prev;
      return ();
    }
    else do {
    ss <- words <$> getLine;
    process n ss key next prev;
    solve (n - 1) key next prev;
    }

process :: Int -> [String] -> IOUArray Int Int -> IOUArray Int Int -> IOUArray Int Int -> IO()
process n ss key next prev
  | command == "insert" = insertx n x key next prev
  | command == "delete" = deletex n x key next prev
  | command == "deleteFirst" = deleteFirst n key next prev
  | command == "deleteLast" = deleteLast n key next prev
  where command = ss !! 0
        x = (read (ss !! 1) :: Int)

printanswer :: IOUArray Int Int -> IOUArray Int Int -> IOUArray Int Int -> IO ()
printanswer key next prev = do
  t <- readArray next 0;
  if t == 0 then do {
    return ();
    }
    else do {
    nextindex <- (readArray next 0);
    x <- readArray key nextindex;
    nextnextindex <- readArray next nextindex;
    putStr (show x);
    printanswer2 nextnextindex key next prev;
    }

printanswer2 :: Int -> IOUArray Int Int -> IOUArray Int Int -> IOUArray Int Int -> IO ()
printanswer2 index key next prev = do
  if index == 0 then
    do {
      putStrLn "";
      return ();
    }
    else do {
    x <- readArray key index;
    putStr (" " ++ (show x));
    nextindex <- (readArray next index);
    printanswer2 nextindex key next prev;
    }

insertx :: Int -> Int -> IOUArray Int Int -> IOUArray Int Int -> IOUArray Int Int -> IO ()
insertx n x key next prev = do
  nextnext <- (readArray next 0);
  writeArray key (n + 1) x;
  writeArray next (n + 1) nextnext;
  writeArray prev (n + 1) 0;
  writeArray prev nextnext (n + 1);
  writeArray next 0 (n + 1);  
  return ();

deletex :: Int -> Int -> IOUArray Int Int -> IOUArray Int Int -> IOUArray Int Int -> IO ()
deletex n x key next prev = do
  nextnext <- readArray next 0;
  deletex2 n nextnext x key next prev;
  return ();

deletex2 :: Int -> Int -> Int -> IOUArray Int Int -> IOUArray Int Int -> IOUArray Int Int -> IO ()
deletex2 n index x key next prev = do
  t <- readArray key index;
  if t == x then
    do {
      pn <- readArray prev index;
      nn <- readArray next index;
      writeArray next pn nn;
      writeArray prev nn pn;
      return ();
      }
    else do {
    nextindex <- readArray next index;
    deletex2 n nextindex x key next prev;
    }

deleteFirst :: Int -> IOUArray Int Int -> IOUArray Int Int -> IOUArray Int Int -> IO ()
deleteFirst n key next prev = do
  nextindex <- readArray next 0;
  nextnextindex <- readArray next nextindex;
  writeArray next 0 nextnextindex;
  writeArray prev nextnextindex 0;

deleteLast :: Int -> IOUArray Int Int -> IOUArray Int Int -> IOUArray Int Int -> IO ()
deleteLast n key next prev = do
  previndex <- readArray prev 0;
  prevprevindex <- readArray prev previndex;
  writeArray prev 0 prevprevindex;
  writeArray next prevprevindex 0;