n = int(input())
a = list(map(int, input().split()))
q = int(input())
ans = sum(a)

l = [0] * 100001

for i in a:
  l[i] += 1
  
for j in range(q):
  b, c = map(int,input().split())
  l[c] += l[b]
  ans += l[b] * (c-b)
  l[b] = 0
  print(ans)
