import collections
N = int(input())
A = [int(x) for x in input().split()]
a = collections.Counter(A)
Q = int(input())
for i in range(Q):
  B, C = map(int, input().split())
  if B in a:
    a[C] += a[B]
    a[B] = 0
  tmp = 0
  for i in a:
    tmp += a[i] * i
  print(tmp)