import qualified Data.ByteString.Char8 as BS
import Data.Char
import Data.List
import qualified Data.IntMap as M

main = do
  BS.getLine
  li <- BS.getLine
  let as = unfoldr (BS.readInt . BS.dropWhile isSpace) li
  BS.getLine
  co <- BS.getContents
  let bcs = map (l2p . unfoldr (BS.readInt . BS.dropWhile isSpace)) $ BS.lines co
  let ans = compute as bcs
  mapM_ print ans

l2p [b,c] = (b,c)

compute :: [Int] -> [(Int,Int)] -> [Int]
compute as bcs = map snd $ tail $ scanl step (am, sum as) bcs
  where
    am = M.fromListWith (+) [ (a,1) | a <- as ]

step :: (M.IntMap Int, Int) -> (Int,Int) -> (M.IntMap Int, Int)
step (am, sum0) (b,c) = (am1, sum1)
  where
    numb = M.findWithDefault 0 b am
    numc = M.findWithDefault 0 c am
    am1 = M.insert c (numb+numc) $ M.delete b am
    sum1 = sum0 + numb * (c - b)
