import Control.Applicative

fromA :: [Int] -> [Int] -> [Int] -> Int -> [Int]
fromA []    []    deckC _ = deckC
fromA []    deckB deckC n = fromB [] deckB deckC n
fromA deckA deckB deckC n = fromB (drop n deckA) deckB (deckC ++ take n deckA) n

fromB :: [Int] -> [Int] -> [Int] -> Int -> [Int]
fromB []    []    deckC _ = deckC
fromB deckA []    deckC n = fromA deckA [] deckC n
fromB deckA deckB deckC n = fromA deckA (drop n deckB) (deckC ++ take n deckB) n


riffle :: [Int] -> Int -> [Int]
riffle deck n =
  let sizeB = length deck `div` 2
      deckA = drop sizeB deck
      deckB = take sizeB deck
  in
      fromA deckA deckB [] n

solver :: [String] -> [[Int]]
solver [] = []
solver (l1:l2:xs) =
  let (x:_) = map (read :: String -> Int) $ words l1
      ys = map (read :: String -> Int) $ words l2
  in
      riffle' [0..(x-1)] ys : solver xs


riffle' :: [Int] -> [Int] -> [Int]
riffle' deck [] = deck
riffle' deck xs = foldl riffle deck xs


main :: IO()
main = do
  contents <- lines <$> getContents
  mapM_ (print . last) (solver contents)
--   contents <- map lines <$> getContents