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