n=int(input())
l=[0]*10**5
A=list(map(int,input().split()))

for a in A:
    l[a-1]+=1

ans=sum(A)

q=int(input())
for i in range(q):
    b,c=map(int,input().split())
    tmp=l[b-1]
    l[b-1]=0
    l[c-1]+=tmp
    
    ans+=tmp*(c-b)
    
    print(ans)