
n=int(input())

alist=[0 for i in range(n+1)]

A=list(map(int,input().split()))
q=int(input())

for i in A:
    alist[i]+=1
asum=sum(A)
for i in range(q):
    b,c=map(int , input().split())
    bnum=alist[b]
    asum+=bnum*(c-b)
    alist[b]=0
    alist[c]+=bnum
    print(asum)
