n=int(input())
ls = list(map(int,input().split()))

ls = sorted(ls)
ans = sum(ls)
newls = [0]*(n+1)
for p in ls:
  newls[p] += 1
ls = newls

q=int(input())
for _ in range(q):
  b,c = map(int,input().split())
  
  tmp = ls[b]
  ans = ans + tmp*c - tmp*b
  ls[b] = 0
  ls[c] = tmp
  
  print(ans)
  
