n=int(input())
a=list(map(int,input().split()))
q=int(input())
cnt=[0 for _ in range(100000)]
for aa in a:
  cnt[aa]+=1
total=0
for i in range(len(cnt)):
  total+= i* cnt[i]
for i in range(q):
  b,c=map(int,input().split())
  cnt[c] += cnt[b]
  total-= cnt[b] * b
  total+= cnt[b] * c
  cnt[b]=0
  print(total)