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

BC = []
i = 0
for i in range(Q):
    BC.append(input().split())

j = 0
k = 0
l = 0
S = 0
for j in range(Q):
    for k in range(N):
        if A[k] == int(BC[j][0]):
            A[k] = int(BC[j][1])
    print(A)
    for l in range(N):
        S = S + A[l]
    print(S)
    S = 0