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