import strutils, sequtils
import algorithm
# import nre,options

let get = iterator: string {.closure.} =
  for s in stdin.readAll.split: yield s
proc read: int = get().parseInt
template rep(p: untyped, n: int): seq = newSeqWith(n, p)

let n = read()
var input_a = read().rep(n)
let q = read()

let bc = read().rep(2).rep(q)

var counts = newSeq[int](n+1)

var sum = 0
for i in input_a:
  counts[i] += 1
  sum += i

var b:int
var c:int
var b_num:int

for i in 0..<q:
  b = bc[i][0]
  c = bc[i][1]
  b_num = counts[b]
  if b_num > 0:
    sum += (c-b)*b_num
    counts[c] += b_num
    counts[b] = 0
  echo sum
