N=int(input())
A=list(map(int,input().split()))
count = [0 for i in range(110000)]
sum_ = sum(A)
for i in range(N):
  count[A[i]] += 1

Q=int(input())
for i in range(Q):
  b,c = map(int,input().split())
  n = count[b]
  sum_ -= b*n
  count[c] += n
  sum_ += c*count[c]
  print(sum_)