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

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

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=0
    for j in range(10**5):
        ans+=l[j]*(j+1)
    print(ans)