import           Control.Monad
import           Control.Monad.ST
import qualified Data.Set                    as S
import qualified Data.Vector.Unboxed         as VU
import qualified Data.Vector.Unboxed.Mutable as VUM

readInt :: IO [Int]
readInt = map read . words <$> getLine

readIntlines :: Int -> IO [[Int]]
readIntlines n = map (map read . words) <$> replicateM n getLine

main :: IO ()
main = do
    [n] <- readInt
    a <- readInt
    [q] <- readInt
    bc <- readIntlines q
    VU.mapM_ print . VU.take q . VU.tail $ solve a bc

solve :: [Int] -> [[Int]] -> VU.Vector Int
solve a bc = runST $ VU.freeze =<< do
    vec <- VUM.new 100001
    ret <- VUM.new 100001
    forM_ a (\x -> do
        n <- (VUM.read vec x)
        VUM.write vec x (n + 1)
        )
    forM_ (zip [1..] bc) (\(i, [b, c]) -> do
        nb <- (VUM.read vec b)
        nc <- (VUM.read vec c)
        VUM.write vec c (nb + nc)
        VUM.write vec b 0
        v <- VU.freeze vec
        let t = VU.ifoldl' (\acc i x -> x * (i+1) + acc) 0 $ VU.tail v
        VUM.write ret i t
        )
    return ret
