N=int(input())
A=list(map(int,input().split()))
A=list(sorted(A))
Q=int(input())
B=[0]*Q
C=[0]*Q
for i in range(0,Q):
    B[i],C[i]=list(map(int,input().split()))
for i in range(0,Q):
    for j in range(0,len(A)):
        if A[j]==B[i]:
            A[j]=C[i]
    print(sum(A))
