N = int(input())
A = list(map(int, input().split()))
Q = int(input())
ans = sum(A)
for i in range(Q):
  b,c = map(int, input().split())
  cnt = A.count(b)
  for i in range(cnt):
    A.remove(b)
    A.append(c)
  ans += (c-b) * cnt
  print(ans)
