n=int(input())
a=list(map(int,input().split()))
q=int(input())
ans=[0]*(10**5+1)
for i in a:
    ans[i]+=1

for i in range(q):
    x=0
    b,c=map(int,input().split())
    ans[c]+=ans[b]
    ans[b]=0
    for j in range(10**5+1):
        if ans[j]==0:
            continue
        x+=(ans[j])*j
    print(x)