N = int(input())
A = list(map(int, input().split()))
Q = int(input())
bc = [map(int, input().split()) for _ in range(Q)]
B, C = [list(i) for i in zip(*bc)]



for i in range(Q):
    
    A = [C[i] if B[i] == j else j for j in A]
    print(A)
    print(sum(A))