n=int(input())
a=list(map(int,input().split()))
co=[0]*(10**5)
s=sum(a)
for i in a:
  co[i-1]+=1
for i in range(int(input())):
  b,c=map(int,input().split())
  s=s+(c-b)*co[b-1]
  print(s)
  co[c-1]+=co[b-1]
  co[b-1]=0