n=int(input())
l=list(map(int,input().split()))
q=int(input())
before=[]
after=[]
for i in range(q):
    a,b=input().split()
    before.append(int(a))
    after.append(int(b))
    
for i in range(q):
  print(before[i])
  print(after[i])
  l=[after[i] if j == before[i] else j for j in l]
  print(sum(l))
