n=int(input())
a=list(map(int,input().split()))
q=int(input())

summ=sum(a)
num=[a.count(i) for i in range(1,100000)]
for i in range(q):
    b,c=map(int,input().split())
    b_num=num[b]
    num[c] += num[b]
    num[b]=0
    summ +=(c-b)*b_num
    print(summ)