_ = int(input())
A = dict()
for a in map(int, input().split()):
  A[a] = A.get(a, 0) + a
  Q = int(input())
  for _ in range(Q):
    b, c = map(int, input().split())
    A[c] = A.get(c, 0) + A.pop(b, 0) // b * c
    print(sum(A.values()))