_n = int(input())
_list = list(map(int, input().split(" ")))
_q = int(input())
for _i in range(_q):
  [_before, _after] = list(map(int, input().split(" ")))
  _sum = 0
  for _j in range(_n):
    if _list[_j] == _before:
      _list[_j] = _after
    _sum += _list[_j]
  print(_sum)