import sys
input = sys.stdin.readline

N=int(input().rstrip('\n'))
As = [int(x) for x in input().rstrip('\n').split()]
res = sum(As)
dis = {i:As.count(i) for i in set(As)}
Q=int(input().rstrip('\n'))
for i in range(Q):
  B,C=(int(x) for x in input().rstrip('\n').split())
  if B in dis:
    n = dis[B]
    if C in dis:
      dis[C] += n
    else:
      dis[C] = n
    res += n*(C-B)
    dis[B] = 0
  print(res)