N = int(input())
A = list(map(int, input().split()))
Q = int(input())

count = [0]*(10**5)
for i in range(len(A)):
  count[A[i]] += 1

A = list(set(A))
for i in range(Q):
  B, C = map(int, input().split())
  count[C] += count[B]
  count[B] = 0
  ans = 0
  for i in range(len(A)):
    ans += count[A[i]] * A[i]
  print(ans)
  