import collections
n = int(input())
a = list(map(int,input().split()))
col = collections.Counter(a)

q = int(input())
s=sum(a)
for i in range(q):
  b,c = map(int,input().split())
  s = s - b * col[b] + c * col[b]
  print(s)

  col[c]+=col[b]
  col[b]=0