n=int(input())
A = list(map(int,input().split()))
B = {}
s = 0
for a in A:
  B[a] = B.get(a,0)+1
  s += a
q=int(input())
for _ in range(q):
  b,c=map(int,input().split())
  s = s - b*B.get(b,0) + c*B.get(b,0)
  B[c] = B.get(c,0)+B.get(b,0)
  B[b] = 0
  print(s)
