N = int(input())
A = list(map(int, input().split()))
Ans = sum(A)
D = [0] * (10**5 + 1)
for i in A:
  D[i] += 1
  
Q = int(input())
for i in range(Q):
  B, C = map(int, input().split())
  D[C] += D[B]
  b = D[B]
  D[B] = 0
  Ans += b * (C - B)
  print(Ans)
  