import qualified Data.Text as T
import qualified Data.Text.IO as T
import qualified Data.Text.Read as T
import Data.IntMap.Strict (IntMap)
import qualified Data.IntMap.Strict as IMS
import Data.List (mapAccumL)

main :: IO ()
main = do
  let
    init :: [Int] -> (Int, IntMap Int)
    init = foldl step (0, IMS.empty)
      where
        step :: (Int, IntMap Int) -> Int -> (Int, IntMap Int)
        step (s, m) x = (s + x, IMS.insertWith (+) x 1 m)
    update :: (Int, IntMap Int) -> [(Int, Int)] -> [Int]
    update sm0 = snd . mapAccumL step sm0
      where
        step :: (Int, IntMap Int) -> (Int, Int) -> ((Int, IntMap Int), Int)
        step (s, m) (b, c) = ((s', m'), s')
          where
            k = IMS.findWithDefault 0 b m :: Int
            s' = s + k * (c - b) :: Int
            m' = IMS.insertWith (+) c k . IMS.delete b $ m :: IntMap Int
  n <- unsafeTextToInt <$> T.getLine :: IO Int
  as <- map unsafeTextToInt . T.words <$> T.getLine :: IO [Int]
  let
    sm0 = init as :: (Int, IntMap Int)
  q <- unsafeTextToInt <$> T.getLine :: IO Int
  bcs <- sequence . replicate q $ unsafeListToPair . map unsafeTextToInt . T.words <$> T.getLine :: IO [(Int, Int)]
  mapM_ print . update sm0 $ bcs

unsafeListToPair :: [a] -> (a, a)
unsafeListToPair [x, y] = (x, y)
{-# INLINE unsafeListToPair #-}

unsafeTextToInt :: T.Text -> Int
unsafeTextToInt s = case T.signed T.decimal s of
  Right (n, _) -> n
{-# INLINE unsafeTextToInt #-}
