N = int(input())
A = list(map(int, input().split()))
ans = sum(A)
Q = int(input())
for i in range(Q):
  B, C = map(int, input().split())
  c = A.count(B)
  ans = ans - c * (B - C)
  A = [C if j == B else j for j in A]
  print(ans)