import numpy as np

n = input()
n = int(n)
a = [e for e in input().split()]
q = input()
q = int(q)

b = np.zeros((q, 2), dtype=int)

for num in range(q):
  b1,b2 = input().split()
  # b[num][0],b[num][1] = input().split()

  ans = 0
  for num2 in range(n):
    if int(a[num2]) == int(b1):
      a[num2] = int(b2)
    ans += int(a[num2])
  print(ans)