N=int(input())
A=list(map(int,input().split()))
TMP=sum(A)
Q=int(input())
import collections
List=collections.Counter(A)
for i in range(Q):
  S,T=map(int,input().split())  
  TMP=TMP+(T-S)*List[S]
  print(TMP)  
  List[T]+=List[S]
  List[S]=0