from collections import Counter
n=int(input())
l=[int(i) for i in input().split()]
sm=sum(l)
z=int(input())
c=Counter(l)
for i in range(z):
    a,b=map(int,input().split())
    sm-=c[a]*a
    sm+=c[a]*b
    c[b]+=c[a]
    c[a]=0
    print(sm)