def csum(c):
  r = 0
  for index, item in enumerate(c):
    r += index * item
  return r

n = int(input())
l= list(map(int,input().split()))
c = [0] * 10001
for i in l:
  c[i] += 1
n = int(input())
for i in range(0, n):
  be,af = map(int,input().split())
  c[af] += c[be]
  c[be] = 0
  print(csum(c))