n = int(input())
a = list(map(int,input().split()))
p = [0 for _ in range(10**5+1)]
for ai in a:
  p[ai] += 1
s = sum(a)

q = int(input())
for i in range(q):
  b,c = map(int,input().split())
  s += p[b]*(c-b)
  print(s)
  p[c] += p[b]
  p[b] = 0