import sys
input = sys.stdin.readline

N=int(input().rstrip('\n'))
As = [int(x) for x in input().rstrip('\n').split()]
Q=int(input().rstrip('\n'))
for i in range(Q):
  B,C=(int(x) for x in input().rstrip('\n').split())
  if B in As:
    cha = [i for i, x in enumerate(As) if x == B]
    for i in cha:
      As[i] = C
  print(sum(As))