import Control.Monad
import qualified Data.ByteString.Char8 as C
import Data.Function
import Data.List
import Data.Ord
import qualified Data.Sequence as Sq
import qualified Data.Vector.Unboxed as U
import qualified Data.Vector.Unboxed.Mutable as UM

r :: Int -> IO (U.Vector Int)
r n = U.unfoldrN n (C.readInt . C.dropWhile (==' ')) <$> C.getLine

r' :: IO [Int]
r'  =   unfoldr    (C.readInt . C.dropWhile (==' ')) <$> C.getLine

main = do
    n <- readLn :: IO Int
    as <- r' :: IO [Int]

    let s = sum as
    let asMapImm = U.accum (+) (U.replicate (10^5+10) 0) (zip as [1,1..]) :: U.Vector Int
    asMap <- U.thaw asMapImm

    q <- readLn :: IO Int

    (_ Sq.:<| res) <- fix (\rec n sq@(ac Sq.:|> s) -> if n == 0 then return sq else do
            [b,c] <- r'
            d <- UM.read asMap b
            v <- UM.read asMap c
            UM.write asMap b 0
            UM.write asMap c (d+v)
            let s' = s - b*d + c*d
            rec (n-1) (sq Sq.|> s')
        ) q (Sq.singleton s)
    mapM_ print res

