N=int(input())
A=list(map(int,input().split()))
Q=int(input())
AA=[0]*(10**5+1)
for i in A:
  AA[i]+=1
for i in range(Q):
  B,C=map(int,input().split())
  AA[C]+=AA[B]
  AA[B]=0
  r=0
  for j in range(len(AA)):
    r+=(j)*AA[j]
  print(r)