
n = int(input())
a = list(map(int, input().split()))
q = int(input())

result = a
sum = sum(result)
l = []
for i in range(q):
  b, c = map(int, input().split())

  sum += result.count(b) * (c-b)
  for j in range(len(result)):
    if result[j] == b:
      result[j] = c
  
  print(sum)
