n=int(input())
a=[int(i) for i in input().split()]
d=[0]*(10**5+1)
for i in a:
    d[i]+=1
ans=sum(a)
q=int(input())
for _ in range(q):
    b,c=map(int,input().split())
    ans=ans-b*d[b]+c*d[b]
    d[b],d[c]=0,d[b]+d[c]
    print(ans)