a = input()
sequence_integer = [int(idx) for idx in input().split()]
num = int(input())
for idx in range(num):
  a, b = [int(idx) for idx in input().split()]
  while a in sequence_integer:
    sequence_integer[sequence_integer.index(a)] = b
  print(sum([int(idx) for idx in sequence_integer]))