n = int(input())
a = list(map(int, input().split()))
q = int(input())
for _ in range(q):
  b, c = map(int, input().split())
  if a.count(b):
    l = a.index(b)
    r = n - a[::-1].index(b)
    for i in range(l, r):
      if a[i] == b:
        a[i] = c
  print(sum(a))
    