n = int(input())
a = [int(i) for i in input().split()]
d = dict()
for i in a:
  if i not in d:
    d[i] = 1
  else:
    d[i] += 1

q = int(input())

res = sum(a)

for i in range(q):
  b, c = [int(i) for i in input().split()]
  if b in d:
    res += (c - b) * d[b]
    if c not in d:
      d[c] = d[b]
    else:
      d[c] += d[b]
  print(res)
