_n = int(input())
_list = list(map(int, input().split(" ")))
_int_list = [0 for _i in range(10**5)]
for _int in _list:
  _int_list[_int] += _int
_q = int(input())

for _i in range(_q):
  [_before, _after] = list(map(int, input().split(" ")))
  _num = _int_list[_before] // _before
  _int_list[_before] = 0
  _int_list[_after] += _after * _num
  print(sum(_int_list))