n = int(input())
a = list(map(int, input().split()))
q = int(input())

s = ','.join(map(str, a)) + ','
ans = []

for _ in range(q):
    b, c = map(str, input().split())
    s = s.replace(b+',', c+',')
    ans.append(s)
    
for i in ans:
    print(i[:-1])
    print(sum(int(str) for str in i[:-1].split(',')))
    