import           Control.Applicative
import           Data.List

splitSet :: [String] -> [[String]]
splitSet xs = unfoldr f xs
  where
    f [] = Nothing
    f (a:b:cs) = Just (a : b : ds, fs)
      where
        (ds, fs) = splitAt (read b) cs

shuffle :: [String] -> String
shuffle (cards:_:hs) = foldl' (\acc h -> one (read h) acc) cards hs
  where
    one :: Int -> String -> String
    one n xs = let (as, bs) = splitAt n xs
                in bs ++ as

main :: IO ()
main = do
    cs <- takeWhile (/= "-") . lines <$> getContents
    let as = fmap shuffle $ splitSet cs
    mapM_ putStrLn as

