import collections
n=int(input())
a=list(map(int,input().split()))
s=sum(a)
q=int(input())
x=collections.Counter(a)
for _ in range(q):
  b,c=map(int,input().split())
  s=s-x[b]*b+x[b]*c
  print(s)
  x[c]+=x[b]
  x[b]=0