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