N=input()
A = list(map(int,input().split()))
Q=int(input())
BC=[]

for j in range(Q):
    BC.append(list(map(int, input().split())))

for i in range(Q):
    if str(BC[i][0]) in str(A):
        ilst= [m for m, x in enumerate(A) if x == BC[i][0]]
#        print("ilst="+str(ilst))
        for j in ilst:
            A[j]=BC[i][1]
#            print("A[j]="+str(A))
#            print(str(BC[i][0]))
#    print("A="+str(A))
    print(sum(A))