import sys
from collections import Counter
readline = sys.stdin.buffer.readline

n = int(readline())
a = Counter(map(int, readline().split()))
q = int(readline())
L = []
for i in range(q):
  L.append(list(map(int, readline().split())))

for b, c in L:
  # print(a)
  if b in a:
    a[c] += a[b]
    del a[b]
  print(sum(x * y for x, y in a.items()))

