num = [0]*100100

n = int(input())
sum = 0
for A in map(int, input().split()):
  num[A] += 1
  sum += A

for _ in range(int(input())):
  b, c = map(int, input().split())
  sum += (c-b)*num[b]
  num[c] += num[b]
  num[b] = 0
  print(sum)