n=int(input())
a=list(map(int,input().split()))
q=int(input())
from collections import Counter
d=Counter(a)
sum=sum(a)
for i in range(q):
    b,c=map(int,input().split())
    tmp=d[b]*(b-c)
    sum-=tmp
    print(sum)
    d[c]+=d[b]
    d[b]=0