n=int(input())
A=list(map(int,input().split()))
Q=int(input())
BC=[list(map(int,input().split())) for _ in range(Q)]

cnt=[0]*(10**5)
for a in A:
  cnt[a]+=1
total=sum(A)
for b,c in BC:
  total+=c*cnt[b]
  total-=b*cnt[b]
  cnt[c]+=cnt[b]
  cnt[b]=0
  print(total)