n=int(input())
a=list(map(int,input().split()))
q=int(input())
l=[0]*q
for i in range(q):
    b,c=map(int,input().split())
    l[i]=(b,c)
book={}
for i in a:
    if i in book:
        book[i]+=1
    else:
        book[i]=1

for i in range(q):
    ans=0
    if l[i][0] in book:
        if l[i][1] in book:
            book[l[i][1]]+=book[l[i][0]]
            book[l[i][0]]=0
        else:
            book[l[i][1]]=book[l[i][0]]
            book[l[i][0]]=0
    for j in book:
        ans+=book[j]*j
    print(ans)