main = do
  [_, w] <- map read . words <$> getLine
  _ <- getLine
  as <- map read . words <$> getLine
  let zs = zip as [0 ..]
  f w (g zs) 0
 
g [] = []
g (z:zs) = (take a . repeat $ show n) ++ g zs
  where [a, n] = z
 
f w cs p
  | cs == [] = putStr ""
  | p == 0 = (putStrLn $ unwords as) >> f w bs 1
  | p == 1 = (putStrLn . unwords $ reverse as) >> f w bs 0
    where (as, bs) = splitAt w cs
