import sys
from collections import Counter
rl=sys.stdin.readline
n=rl()
A=list(map(int,rl().split()))
q=int(rl())
D=Counter(A)
s=sum(A)
for _ in range(q):
  b,c=map(int,rl().split())
  s+=(c-b)*D[b]
  D[c]+=D[b]
  del D[b]
  print(s)