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