N=int(input())
A=list(map(int,input().split()))
Q=int(input())
somme=sum(A)

D={x:A.count(x) for x in set(A)}

for step in range(Q):
    b,c=map(int,input().split())
    if b not in D:
        print(somme)
        continue

    somme=somme+(c-b)*D[b]
    if c in D:
        D[c]=D[c]+D[b]
    else:
        D[c]=D[b]
    del D[b]    
    print(somme)