n=int(input())
A=list(map(int,input().split()))

C=[0]*(10**5+1)
for a in A:
    C[a]+=1

ans=sum(A)
q=int(input())
for i in range(q):
    b,c=map(int,input().split())
    d=C[b]
    C[c]+=d
    C[b]=0
    ans+=d*(c-b)
    print(ans)