N=int(input())
A=list(map(int,input().strip().split()))
Q=int(input())

dp=[0 for n in range(100000)]
for n in range(len(A)):
    dp[A[n]-1]+=1

for n in range(Q):
    B,C=map(int,input().strip().split())
    dp[C-1]+=dp[B-1]
    dp[B-1]=0

    ans=0
    for n in range(1,len(dp)):
        ans+=n*dp[n-1]
    print(ans)