n=int(input())
a=list(map(int, input().split()))
q=int(input())
f=[0]*(100001)
s=0
for i in a:
    f[i]+=1
    s+=i
for _ in range(q):
    b,c=map(int, input().split())
    s-=f[b]*b
    f[c]+=f[b]
    s+=c*f[b]
    f[b]=0
    print(s)
