import Control.Monad
import qualified Data.ByteString.Char8 as BS
import Data.List
import Data.Maybe

readInt = fst . fromJust . BS.readInt

readIntList = map readInt . BS.words

getInt = readInt <$> BS.getLine

getIntList = readIntList <$> BS.getLine

getIntNList n = map readIntList <$> replicateM (fromIntegral n) BS.getLine

elemCountOnce :: Eq a => [a] -> [(a, Int)]
elemCountOnce [] = []
elemCountOnce (x : xs) = (x, (length $ filter (== x) xs) + 1) : elemCountOnce (filter (/= x) xs)

calcAll as (bc : bcs) = do
  let as' = map (\a -> (if fst a == head bc then (last bc, snd a) else (fst a, snd a))) as
  print $ sum (map (\a -> (fst a) * (snd a)) as')
  when (bcs /= []) $ calcAll as' bcs

main = do
  n <- getInt
  as <- getIntList
  q <- getInt
  bcs <- getIntNList q
  let as' = elemCountOnce as
  calcAll as' bcs
