import Text.Regex.PCRE
import Data.List
import System.Environment

main :: IO ()
main = do
  input <- getContents
  args  <- getArgs
  let pass = args !! 0
  putStrLn . scramble pass . lines $ input

scramble pass [] = pass
scramble pass (l : ls) = scramble pass' ls
  where
    action = head [ a | (p, a) <- rules, l =~ p]
    pass' = action pass l

rules = [r1, r2, r3, r4, r5, r6]

r1 = (pattern, action)
  where
    pattern = "swap position (\\d+) with position (\\d+)"
    action :: String -> String -> String
    action pass line = pre ++ [cy] ++ mid ++ [cx] ++ end
      where
        [[_, xs, ys]] = line =~ pattern :: [[String]]
        [x, y] = sort [read xs, read ys]
        (pre, (cx : rest)) = splitAt x pass
        (mid, (cy : end))  = splitAt (y - x - 1) rest

r2 = (pattern, action)
  where
    pattern = "swap letter (\\w) with letter (\\w)"
    action :: String -> String -> String
    action pass line = map swapper pass
      where
        [[_, xs, ys]] = line =~ pattern :: [[String]]
        [x, y] = map head [xs, ys]
        swapper z = if z == x
                    then y
                    else if z == y
                         then x
                         else z

r3 = (pattern, action)
  where
    pattern = "rotate (left|right) (\\d+) steps?"
    action :: String -> String -> String
    action pass line = if dir == "left"
                       then let (l, r) = splitAt n pass in r ++ l
                       else let (l, r) = splitAt (length pass - n) pass
                            in r ++ l
      where
        [[_, dir, ns]] = line =~ pattern :: [[String]]
        n = read ns `mod` length pass

r4 = (pattern, action)
  where
    pattern = "rotate based on position of letter (\\w)"
    action :: String -> String -> String
    action pass line = let (l, r) = splitAt b pass in r ++ l
      where
        [[_, xs]] = line =~ pattern :: [[String]]
        x = head xs
        i = head $ elemIndices x pass
        r = if i >= 4
            then i + 2
            else i + 1
        b = length pass - (r `mod` length pass)

r5 = (pattern, action)
  where
    pattern = "reverse positions (\\d+) through (\\d+)"
    action :: String -> String -> String
    action pass line = pre ++ reverse mid ++ end
      where
        [[_, xs, ys]] = line =~ pattern :: [[String]]
        [x, y] = sort . map read $ [xs, ys]
        (pre, rest) = splitAt x pass
        (mid, end)  = splitAt (y - x + 1) rest

r6 = (pattern, action)
  where
    pattern = "move position (\\d+) to position (\\d+)"
    action :: String -> String -> String
    action pass line = l ++ [xc] ++ r
      where
        [[_, xs, ys]] = line =~ pattern :: [[String]]
        [x, y] = map read [xs, ys]
        xc = pass !! x
        pass' = delete xc pass
        (l, r) = splitAt y pass'
