import strutils
 
let read = iterator: string =
  while true: (for s in stdin.readLine.split: yield s)
 
template input*(T: static[typedesc]): untyped = 
  when T is int: read().parseInt
  elif T is float: read().parseFloat
  elif T is string: read()

import sequtils, tables

let N = input(int)
let A = newSeqWith(N, input(int))
var sum = 0
var count = initCountTable[int]()
for a in A:
  sum += a
  count.inc(a, 1)

var res = newSeq[int]()
let Q = input(int)
for _ in 1 .. Q:
  let b, c = input(int)
  let d = count.getOrDefault(b)
  if d != 0:
    count.inc(c, d)
    count.del(b)
    sum += (c - b) * d
  res.add(sum)
echo res.join("\n")