import sys
input = sys.stdin.readline
n=int(input())
A=list(map(int,input().split()))
k={}
ans=sum(A)
for i in A:
  if i in k:
    k[i]+=1
  else:
    k[i]=1
q=int(input())
for i in range(q):
  b,c=map(int,input().split())
  if b in k:
    f=c*k[b]-b*k[b]
    ans+=f
    del k[b]
    if c in k:
      k[c]+=1
    else:
      k[c]=1
  print(ans)