import collections
N=int(input())
A=list(map(int,input().split()))
Q=int(input())
d=collections.Counter(A)
for i in range(Q):
  B,C=map(int,input().split())
  if d[C] in d:
    d[C]+=d[B]
  else:
    d[C]=d[B]
  d[B]=0
  ans=0
  for j in d:
    ans += j*d[j]
  print(ans)