from collections import deque
from bisect import bisect_left
n=int(input())
a=sorted(list(map(int,input().split())))

sus=deque([sum(a)])
q=int(input())
for i in range(q):
  count=0
  b,c=map(int,input().split())
  idx=bisect_left(a,b)
  while True:
    if idx>=n:break
    if a[idx]==b:
      a[idx]=c
      count+=1
      idx+=1
    else:
      break
  a=sorted(a)
  sus.append(sus[-1]+(c-b)*count)
  print(sus[-1])