N=int(input())
*A,=map(int,input().split())
A.sort()
Q=int(input())
bc=[list(map(int,input().split()))for _ in range(Q)]

from collections import*
C=Counter(A)
ans=sum(A)

for b,c in bc:
    C[b],C[c],ans=0,C[c]+C[b],ans-b*C[b]+c*C[b]
    print(ans)