n=int(input())
ar=list(map(int,input().split()))
di={}
for i in range(n):
    if ar[i] not in di:
        di[ar[i]]=1
    else:
        di[ar[i]]+=1
q=int(input())
for i in range(q):
    b,c=map(int,input().split())
    if(b in di):
        x=di[b]
        del di[b]
        if(c in di):
            di[c]+=x
        else:
            di[c]=x
    s=0
    for j in di.keys():
        s+=j*(di[j])
    print(s)

        
        
    