n=int(input())
a=list(map(int,input().split()))
ans=[0 for i in range(10001)]
for i in range(n):
    ans[a[i]-1]+=1
tmp=0
for i in range(10001):
    tt=(i+1)*ans[i]
    tmp+=tt
q=int(input())
for i in range(q):
    b,c=map(int,input().split())
    tt=-ans[b-1]*b+ans[b-1]*c
    ans[c-1]+=ans[b-1]
    ans[b-1]=0
    tmp+=tt
    print(tmp)