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