from collections import defaultdict
n = int(input())
A=list(map(int,input().split()))
d = defaultdict(int)
for a in A:
  d[a]+=1
ans = sum(A)
for _ in range(int(input())):
  b,c=map(int,input().split())
  d[c] += d[b]
  d[b] = 0
  ans += (c-b)*d[b]
  print(ans)