s=set()
l = [0] * ( 10 ** 5 + 1 )
N = int(input())
A = list(map(int, input().split()))
for i in range(len(A)):
  l[A[i]] += 1
  s.add(A[i])
Q = int(input())
for i in range(Q):
  B, C = map(int, input().split())
  l[C] += l[B]
  s.add(C)
  s.discard(B)
  sum=0
  for i in s:
    sum += i * l[i]
