N = int(input())
A = [0 for _ in range(100001)]
a = list(map(int,input().split()))
ans = 0
for i in a:
  A[i] += 1
  ans += i
Q = int(input())
for i in range(Q):
  B,C = map(int,input().split())
  ans += (C -B) * A[B]
  A[C] += A[B]
  A[B] = 0
  print(ans)
  