N = int(input())
A = map(int, input().split())
Q = int(input())

from collections import defaultdict
count = defaultdict(int)

for a in A:
  count[a] += 1

for _ in range(Q):
  B, C = map(int, input().split())
  if count[B] > 0:
    count[C] += count[B]
    count[B] = 0

# print(count)
ans = 0
for k, v in count.items():
  ans += k * v

print(ans)