import sys
input = sys.stdin.readline
n = int(input())
a = list(map(int,input().split()))
num = [0]*(10**5+1)
for i in a:
  num[i] += 1
sm = sum(a)
q = int(input())
for _ in range(q):
  x,y = map(int,input().split())
  nx = num[x]
  ny = num[y]
  sm = sm+x*ny+y*nx-x*nx-y*ny
  print(sm)
  num[x] = ny
  num[y] = nx