import Data.List
import Data.Char

main = interact $ show' . solve [] . read'
  where
    show' = unlines . reverse
    solve acc [] = acc
    solve acc (s:n:xs) = solve ((solveOne init' s):acc) tail'
      where
        (init', tail') = splitAt (readInt n) xs
        solveOne [] s = s
        solveOne (y:ys) s = solveOne ys (st ++ sh)
          where (sh, st) = splitAt (readInt y) s
    read' = takeWhile (/= "-") . lines
    readInt = read :: String -> Int

