from collections import Counter
 
n = int(input())
a = list(map(int,input().split()))
q = int(input())
 
ans=sum(a)
A=Counter(a)

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