from collections import Counter
cnt=Counter()
n=int(input())
ar=list(map(int,input().split()))

sum=0
for i in ar:
    cnt[i]+=cnt[i]+1;
    sum+=i;
q=int(input())
for i in range(0,q):
    a,b=map(int,input().split(' '))
    sum-=cnt[a]*a
    sum+=cnt[a]*b
    cnt[b]+=cnt[a]
    cnt[a]=0
    print(sum)
