
n=int(input())
arr=list(map(int,input().split()))
sumv=0
d={}
for i in arr:
    sumv+=i
    try:
        d[i]+=1
    except:
        d[i]=1

q=int(input())

while q:
    
    b,c=map(int,input().split())
    sumv+=(c-b)*d[b]
    try:
        d[c]+=d[b]
    except:
        d[c]=d[b]
    d[b]=0
    print(sumv)


    q-=1
