N=int(input())
A=list(map(int,input().split()))
X=[0 for i in range(10**5+1)]
Q=int(input())
sum_=sum(A)
for i in range(N):
    X[A[i]]+=1
for i in range(Q):
    B,C=map(int,input().split())
    sum_+=X[B]*(C-B)
    X[C]+=X[B]
    X[B]=0
    print(sum_)