import Data.List

main :: IO ()
main = do
    _ <- getLine 
    xs <- map (read :: String -> Int) . words <$> getLine
    q <- readLn
    let xs' = [(head ys, length ys) | ys <- group xs]
    analyze q xs'
        where
            analyze n xs = do
                [b, c] <- map read . words <$> getLine
                let replicated = repTarget b c xs
                putStrLn $ show $ sum $ map (\x -> fst x * snd x) replicated
                if n == 1
                    then return ()
                    else analyze (n - 1) replicated
            repTarget cur next [] = []
            repTarget cur next ((b, c):xs)
                | b == cur = (next, c) : (repTarget cur next xs)
                | otherwise = (b, c) : (repTarget cur next xs)