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

for i in range(q):
  b, c = map(int, input().split())
  cnt = 0
  for j in range(len(a)):
    if a[j] == b:
      a[j] = c
      cnt += 1
  x += (c - b) * cnt
  print(x)