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