import collections
N = int(input())
A = list(map(int, input().split()))
indices = collections.defaultdict(list)
for i, a in enumerate(A):
  indices[a].append(i)
Q = int(input())
for _ in range(Q):
  B, C = map(int, input().split())
  print(B, C)
  for i in indices[B]:
    indices[C].append(i)
    A[i] = C
  print(sum(A))