n=int(input())
lis=list(map(int,input().split()))
hoge=[0 for i in range(10+1)]
hogesum=[0 for i in range(10+1)]
for i in range(n):
  hoge[lis[i]]+=1
  hogesum[lis[i]]+=lis[i]
m=int(input())
for i in range(m):
  a,b=map(int,input().split())
  hoge[b]+=hoge[a]
  hoge[a]=0
  hogesum[a]=0
  hogesum[hoge[b]]=b*hoge[b]
  print(sum(hogesum))