n = int(input())
arr = list(map(int,input().split()))
t = int(input())
while t:
    t-=1
    k,r = map(int,input().split())
    arr.sort()
    if k in arr:
        for i in range(n):
            if arr[i]>k:
                break
            if arr[i]==k:
                arr[i]=r
            
    print(arr)
    print(sum(arr))