main :: IO ()
main = do
  n <- readLn :: IO Int
  ts <- map read . words <$> getLine :: IO [Int]
  m <- readLn :: IO Int
  ds <- getDrinks m :: IO [(Int, Int)]
  prints $ spec ds ts 

getDrinks :: Int -> IO [(Int, Int)]
getDrinks m = do
  ns <- sequenceA (replicate m (map read . words <$> getLine :: IO [Int])) :: IO [[Int]]
  return (map (\n -> (head n, last n)) ns)

charged :: [Int] -> (Int, Int) -> [Int]
charged ts d =
  (reverse $ tail $ reverse $ (take p ts)) ++ [x] ++ (drop p ts)
  where p = fst d
        x = snd d

spec :: [(Int, Int)] -> [Int] -> [Int]
spec ds ts = map (\d -> sum $ charged ts d) ds

prints :: [Int] -> IO ()
prints [] = return ()
prints (n:ns) = do
  print n 
  prints ns
