N=int(input())
A=list(map(int,input().split()))
Q=int(input())
dp=[0]*(10**5+1)
ans=sum(A)
for i in range(N):
  dp[A[i]]+=1
for _ in range(Q):
  B,C=map(int,input().split())
  dp[C]+=dp[B]
  ans+=dp[B]*(C-B)
  dp[B]=0
  print(ans)