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'))
Qs=[]
for i in range(Q):
  Qs.append(tuple(int(x) for x in input().rstrip('\n').split()))
for B,C in Qs:
  if B in dis:
    res += dis[B]*(C-B)
    if C in dis:
      dis[C] += dis[B]
    else:
      dis[C] = dis[B]
    dis[B]=0
  print(res)