import sys
input = sys.stdin.readline

N=int(input().rstrip('\n'))
As = [int(x) for x in input().rstrip('\n').split()]
dis = {}
for i in As:
  if i in dis:
    dis[i] += 1
  else:
    dis[i] = 1
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:
    if C in dis:
      dis[C] += dis[B]
      del dis[B]
    else:
      dis[C] = dis[B]
      del dis[B]
  print(sum(i*x for i,x in dis.items()))